Apache HttpClient - 默认协议

时间:2014-10-23 07:57:32

标签: apache http https protocols apache-httpclient-4.x

我正在使用 Apache HttpClient 发送 POST 请求。如何确定我的 Apache HttpClient 实例用于发送“https://”请求的PROTOCOL。我使用以下代码块发送我的POST请求。

    public void sendPostURL(String url, HashMap<String, String>params, String user, String pass) {
     HttpClient client = new HttpClient();
    String urlContent = "";
    PostMethod method = new PostMethod("https://...");

    // Prepare connection information                                                                                                                          
    client.getParams().setParameter("http.useragent", "MyApp");


    if ( (user != null) &&(pass != null) ) {
            client.getParams().setAuthenticationPreemptive(true);
            client.getState().setCredentials(AuthScope.ANY, (new UsernamePasswordCredentials(user, pass)));
    }


    // Prepare parameters                                                                                                                                      
    for (Map.Entry<String, String> entry : params.entrySet()) {
            method.addParameter(entry.getKey(), ((entry.getValue() != null) ? entry.getValue().toString() : ""));
    }

    try{
            // HTTP execution                                                                                                                                  
        int returnCode = client.executeMethod(method);



    } catch (Exception e) {
            // Exception                                                                                                                                       
            e.printStackTrace();
    } finally {
        method.releaseConnection();
    }

    }

请指导我如何获得HttpClient用于发送请求的 PROTOCOL 。另外,我如何覆盖使用的 PROTOCOL 。希望有一个解决方案。提前谢谢。

1 个答案:

答案 0 :(得分:0)

协议 HTTPS,不是吗?