go tls_client HttpClient 的使用

20 min read

使用HttpClient发送HTTPS请求需要做以下几个步骤:

  1. 创建HttpClient对象,可以使用默认的构造函数创建,也可以添加参数进行配置(如代理服务器,连接超时时间等)。
HttpClient httpClient = new HttpClient();
  1. 创建TLS协议对象,指定TLS版本号和加密套件,并注册到HttpClient中。
Protocol protocol = new Protocol("TLS", new TrustAllSSLSocketFactory(), TLS_PROTOCOL_VERSION);
httpClient.getState().getHttpConnectionManager().getParams().setBooleanParameter("http.protocol.expect-continue", false);
Protocol.registerProtocol("https", protocol);

其中,TrustAllSSLSocketFactory是一个自定义的SocketFactory,用于信任所有证书(忽略证书验证),TLS_PROTOCOL_VERSION为指定的TLS版本号(如TLSv1.2)。

  1. 创建HttpPost对象,指定请求的URL和请求参数。
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1", "value1"));
params.add(new BasicNameValuePair("param2", "value2"));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
  1. 执行请求,并获取返回结果。
HttpResponse httpResponse = httpClient.execute(httpPost);
String responseString = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");

上述代码中,httpClient.execute(httpPost)方法会阻塞等待服务器响应,直到超时或收到响应为止。获取响应数据后,使用EntityUtils将响应实体转化为字符串格式。

完整示例代码:

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.HttpPost;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.util.EntityUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TLSClientExample {

    private static final String URL = "https://example.com/api/";

    private static final String TLS_PROTOCOL_VERSION = "TLSv1.2";

    public static void main(String[] args) throws HttpException, IOException {
        HttpClient httpClient = new HttpClient();

        // Create TLS protocol object with a TrustAllSSLSocketFactory and register it with HttpClient
        TrustAllSSLSocketFactory trustAllSSLSocketFactory = new TrustAllSSLSocketFactory();
        Protocol protocol = new Protocol("https", trustAllSSLSocketFactory, TLS_PROTOCOL_VERSION);

        httpClient.getState().getHttpConnectionManager().getParams().setBooleanParameter("http.protocol.expect-continue", false);
        Protocol.registerProtocol("https", protocol);

        // Create a POST method for the target URL and configure the parameters
        HttpPost httpPost = new HttpPost(URL);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("param1", "value1"));
        params.add(new BasicNameValuePair("param2", "value2"));
        httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

        // Execute the POST method and process the response
        httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");
        HttpResponse httpResponse = httpClient.execute(httpPost);
        String responseString = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");

        System.out.println(responseString);
    }
}

注意,上述代码中的TrustAllSSLSocketFactory是一个自定义的SocketFactory,用于信任所有证书(忽略证书验证)。在实际生产环境中,建议使用合法的证书,或者配置自签名证书来保证通信的安全性。