大家好,欢迎来到IT知识分享网。
有的时候需要在电脑上搭建Http代理服务器。本篇文章是在 Windows + CCProxy搭建的网络代理。
只需要勾选http选项,如下图所示。第一步
第二步
Java端http代理设定.需要引用的jar包:httpclient-4.3.1.jar、httpcore-4.3.2.jar:代理服务器地址:127.0.0.1:9988
实际请求地址为:127.0.0.1:7001地址
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* 请求第三方http代理<br>
* demo
*/
public class CopyOfRstHttpClientProxy {
public static final CopyOfRstHttpClientProxy Me = new CopyOfRstHttpClientProxy();
private static final String PLAT_STREAM_ENCODE = "UTF8";
public static PoolingHttpClientConnectionManager cm = null;
static {
cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);
cm.setDefaultMaxPerRoute(50);
}
/**
* http请求数据代理
*
* @param content
* @return
*/
public String submit(String request) {
// HttpClient
CloseableHttpClient closeableHttpClient = HttpClients.custom().setConnectionManager(cm).build();
// 依次是目标请求地址,端口号,协议类型
HttpHost target = new HttpHost("127.0.0.1", 7001, "http");
// 依次是代理地址,代理端口号,协议类型
HttpHost proxy = new HttpHost("127.0.0.1", 9988, "http");
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
// 请求地址
HttpPost httpPost = new HttpPost("http://127.0.0.1:7001/Demo/servlet");
httpPost.setConfig(config);
// 创建参数队列-Form表单格式
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("Xml", request));// Xml:加密后的XML交易报文
formparams.add(new BasicNameValuePair("Sid", "01"));// Sid:请求服务ID(原文)
UrlEncodedFormEntity entity = null;
String result = "";
try {
// StringEntity形式
// StringEntity entity = new
// StringEntity(request,PLAT_STREAM_ENCODE);
entity = new UrlEncodedFormEntity(formparams, PLAT_STREAM_ENCODE);
httpPost.setEntity(entity);
CloseableHttpResponse response = closeableHttpClient.execute(target, httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
result =EntityUtils.toString(httpEntity, PLAT_STREAM_ENCODE);
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/10491.html