java使用代理ip和端口访问第三方接口()

关于CookieSpecs可以查看这篇文章:HTTP状态管理

关于ConnectionRequestTimeout、SocketTimeout、ConnectTimeout可以查看这篇文章:ConnectionRequestTimeout、SocketTimeout、ConnectTimeout的含义

关于CloseableHttpClient的优化可以查看这篇文章:CloseableHttpClient的使用和优化

//proxyIp代理ip,proxyPort代理端口
HttpHost proxy=new HttpHost(proxyIp,proxyPort);
//设置请求配置
RequestConfig config=RequestConfig.custom()
        .setCookieSpec(CookieSpecs.STANDARD)//cookie管理的规范
        //.setConnectionRequestTimeout(2000)httpclient使用连接池来管理连接,这个时间就是从连接池获取连接的超时时间
        .setSocketTimeout(2000)//设置数据传输过程中数据包之间间隔的最大时间
        .setConnectTimeout(2000)//连接建立时间,三次握手完成时间
        .setProxy(proxy)
        .build();

CloseableHttpClient httpclient=HttpClients.custom()
        .setProxy(proxy)
        .setDefaultRequestConfig(config)
        .build();
        

//调用get,url为真正访问的url,如:http://ip:8080/api
String getUrl=url+params;
HttpGet httpGet=new HttpGet(getUrl);
HttpResponse response=httpclient.execute(httpGet);
HttpEntity httpEntity=response.getEntity();
if(httpEntity!=null){
    //响应为文件,转成BASE64
    byte[] fileByte=EntityUtils.toByteArray(httpEntity);
    String res=DatatypeConverter.printBase64Binary(fileByte);
}


//调用post,url为真正访问第三方的url,如:http://ip:8080/api
HttpPost httpPost=new HttpPost(url);
//请求头设置域名:访问ip和端口
httpPost.setHeader(HttpHeaders.HOST,hostName);
httpPost.setHeader(HttpHeaders.ACCEPT,"*/*");
httpPost.setHeader(HttpHeaders.CONTENT_TYPE,"application/json");
StringEntity entity=new StringEntity(params,"UTF-8");
httpPost.setEntity(entity);
HttpResponse response=httpclient.execute(httpPost);
HttpEntity httpEntity=response.getEntity();
if(httpEntity!=null){
    String res=EntityUtils.toString(httpEntity,"UTF-8");
}
————————

关于CookieSpecs可以查看这篇文章:HTTP状态管理

关于ConnectionRequestTimeout、SocketTimeout、ConnectTimeout可以查看这篇文章:ConnectionRequestTimeout、SocketTimeout、ConnectTimeout的含义

关于CloseableHttpClient的优化可以查看这篇文章:CloseableHttpClient的使用和优化

//proxyIp代理ip,proxyPort代理端口
HttpHost proxy=new HttpHost(proxyIp,proxyPort);
//设置请求配置
RequestConfig config=RequestConfig.custom()
        .setCookieSpec(CookieSpecs.STANDARD)//cookie管理的规范
        //.setConnectionRequestTimeout(2000)httpclient使用连接池来管理连接,这个时间就是从连接池获取连接的超时时间
        .setSocketTimeout(2000)//设置数据传输过程中数据包之间间隔的最大时间
        .setConnectTimeout(2000)//连接建立时间,三次握手完成时间
        .setProxy(proxy)
        .build();

CloseableHttpClient httpclient=HttpClients.custom()
        .setProxy(proxy)
        .setDefaultRequestConfig(config)
        .build();
        

//调用get,url为真正访问的url,如:http://ip:8080/api
String getUrl=url+params;
HttpGet httpGet=new HttpGet(getUrl);
HttpResponse response=httpclient.execute(httpGet);
HttpEntity httpEntity=response.getEntity();
if(httpEntity!=null){
    //响应为文件,转成BASE64
    byte[] fileByte=EntityUtils.toByteArray(httpEntity);
    String res=DatatypeConverter.printBase64Binary(fileByte);
}


//调用post,url为真正访问第三方的url,如:http://ip:8080/api
HttpPost httpPost=new HttpPost(url);
//请求头设置域名:访问ip和端口
httpPost.setHeader(HttpHeaders.HOST,hostName);
httpPost.setHeader(HttpHeaders.ACCEPT,"*/*");
httpPost.setHeader(HttpHeaders.CONTENT_TYPE,"application/json");
StringEntity entity=new StringEntity(params,"UTF-8");
httpPost.setEntity(entity);
HttpResponse response=httpclient.execute(httpPost);
HttpEntity httpEntity=response.getEntity();
if(httpEntity!=null){
    String res=EntityUtils.toString(httpEntity,"UTF-8");
}