【问题标题】:Call HTTPS web service from android device with header to validate从带有标头的 android 设备调用 HTTPS Web 服务以进行验证
【发布时间】:2014-08-16 23:07:50
【问题描述】:

我想从 Android 设备调用 HTTPS 网络服务。其中包含用于验证服务器上数据的标头。我不知道如何调用用 java 编写的 https 网络服务。如果有人解释可能对我有帮助的步骤。谢谢你!

【问题讨论】:

    标签: android rest https


    【解决方案1】:

    请检查此方法:

    private static Header[] headers;
    private String executeRequest(Httpget request)
            throws SocketTimeoutException {
        String responce = null;
    
        try {
    
            HttpParams httpParameters = new BasicHttpParams();
            // Set the timeout in milliseconds until a connection is
            // established.
            int timeoutConnection = 120000;
            HttpConnectionParams.setConnectionTimeout(httpParameters,
                    timeoutConnection);
            // Set the default socket timeout (SO_TIMEOUT)
            // in milliseconds which is the timeout for waiting for data.
            int timeoutSocket = 120000;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
            HttpClient httpclient = new DefaultHttpClient(httpParameters);
            HttpResponse httpResponse = httpclient.execute(request);
            // Obtain and Set Headers when login
            headers = null;
            headers = httpResponse.getAllHeaders();
            responseCode = httpResponse.getStatusLine().getStatusCode();
            responce = EntityUtils.toString(httpResponse.getEntity());
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return responce;
    
    }
    
    Header previousHeader;
    
    // used to add headers manually that we got from the response but now it is
    // done automatically
    @SuppressWarnings("unused")
    private HttpUriRequest addHeaders(HttpUriRequest request) {
        for (int i = 0; headers != null && i < headers.length; i++) {
    
                request.setHeader(headers[i].getName(), headers[i].getValue());
    
        }
        // request.addHeader("Content-Length", "");
        return request;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多