从android设备调用HTTPS Web服务,并使用标头进行验证

时间:2014-06-26 07:27:28

标签: android rest https

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

1 个答案:

答案 0 :(得分:0)

请检查此方法:

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;

}