//研究了一下午,终于发现一个问题,写这个代码不是很难的,难的是找一个能代理的IP地址,实际网上的好多代码都可以用滴,只是代理IP和Port有问题而已,废话少说,直接上代码:
1、先说Post的代理
//HttpClientPostProxyServlet
package com.abin.lee.servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
* Created with IntelliJ IDEA.
* User: abin
* Date: 13-4-18
* Time: 上午8:39
* To change this template use File | Settings | File Templates.
*/
public class HttpClientPostProxyServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException {
System.out.println("receive httpPost request");
String userName=request.getParameter("userName");
String passWord=request.getParameter("passWord");
String localIp=request.getLocalAddr();
String localName=request.getLocalName();
int localPort=request.getLocalPort();
int serverPort=request.getServerPort();
String leeHeader=request.getHeader("lee");
System.out.println("userName="+userName+",passWord="+passWord+",localIp="+localIp+",localName="+localName+",localPort="+localPort);
System.out.println("serverPort="+serverPort+",leeHeader="+leeHeader);
String remoteIp=request.getRemoteAddr();
String remoteHost=request.getRemoteHost();
int remotePort=request.getRemotePort();
String remoteUser=request.getRemoteUser();
System.out.println("remoteIp="+remoteIp+",remoteHost="+remoteHost+",remotePort="+remotePort+",remoteUser="+remoteUser);
ServletOutputStream out=response.getOutputStream();
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out));
writer.write("success");
writer.flush();
writer.close();
}
}
Post代理测试类:
//HttpClientPostProxyServletTest.java
package com.abin.lee.ssh.senior.proxy.httpclient;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class HttpClientPostProxyServletTest {
public static final String HttpPostProxyUrl="http://localhost:8100/MyThread/HttpClientPostProxyServlet";
@Test
public void testHttpClientPostProxyServlet()throws Exception {
HttpHost proxy = new HttpHost("10.10.10.10", 1443, "http");
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpHost target = new HttpHost("localhost", 8100, "http");
HttpPost request = new HttpPost(HttpPostProxyUrl);
StringEntity reqEntity = new StringEntity("userName=abin&passWord=varyall");
reqEntity.setContentType("application/x-www-form-urlencoded");
request.setEntity(reqEntity);
request.setHeader("lee", "lee");
System.out.println("executing request to " + target + " via " + proxy);
HttpResponse rsp = httpclient.execute(target, request);
HttpEntity entity = rsp.getEntity();
System.out.println("----------------------------------------");
System.out.println(rsp.getStatusLine());
Header[] headers = rsp.getAllHeaders();
for (int i = 0; i<headers.length; i++) {
System.out.println(headers[i]);
}
System.out.println("----------------------------------------");
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}
//正常测试代码,非代理
package com.abin.lee.ssh.senior.proxy.httpclient;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Test;
public class HttpClientPostServletTest {
public static final String HttpPostUrl="http://localhost:8100/MyThread/HttpClientPostProxyServlet";
@Test
public void testHttpClientPostServlet(){
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(HttpPostUrl);
try {
// 目标地址
System.out.println("请求: " + httpPost.getRequestLine());
// 构造最简单的字符串数据
StringEntity reqEntity = new StringEntity("userName=abin&passWord=varyall");
// 设置类型
reqEntity.setContentType("application/x-www-form-urlencoded");
// 设置请求的数据
httpPost.setEntity(reqEntity);
httpPost.setHeader("lee", "lee");
// 执行
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
// 显示结果
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(!httpPost.isAborted()){
httpPost.abort();
}
httpClient.getConnectionManager().shutdown();
}
}
}
//servlet配置
<servlet>
<servlet-name>HttpClientPostProxyServlet</servlet-name>
<servlet-class>com.abin.lee.servlet.HttpClientPostProxyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HttpClientPostProxyServlet</servlet-name>
<url-pattern>/HttpClientPostProxyServlet</url-pattern>
</servlet-mapping>
我这里的IP和端口不一定能用哟,自己找能用的!!代码是100%没问题的,经过生产环境测试的哟!!!