//研究了一下午,终于发现一个问题,写这个代码不是很难的,难的是找一个能代理的IP地址,实际网上的好多代码都可以用滴,只是代理IP和Port有问题而已,废话少说,直接上代码:
1、先说Get的代理(首先提供一个Servlet的Get的http服务)
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 HttpClientGetProxyServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException {
System.out.println("receive httpGet 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();
System.out.println("remoteIp="+remoteIp+",remoteHost="+remoteHost+",remotePort="+remotePort);
ServletOutputStream out=response.getOutputStream();
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out));
writer.write("success");
writer.flush();
writer.close();
}
}
HttpGet代理测试类:
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.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class HttpClientGetProxyServletTest {
public static final String HttpGetProxyUrl="http://localhost:8100/MyThread/HttpClientGetProxyServlet";
@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");
HttpGet request = new HttpGet(HttpGetProxyUrl+"?"+"userName=abin&passWord=varyall");
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.HttpGet;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Test;
public class HttpClientGetServletTest {
public static final String HttpGetUrl = "http://localhost:8100/MyThread/HttpClientGetProxyServlet";
@Test
public void HttpClientGetServlet() {
HttpClient httpClient = new DefaultHttpClient();
StringEntity reqEntity = null;
HttpGet httpGet = null;
try {
HttpGet request = new HttpGet(HttpGetUrl+"?"+"userName=abin&passWord=varyall");
request.setHeader("lee", "lee");
// 目标地址
System.out.println("请求: " + httpGet.getRequestLine());
// 执行
HttpResponse response = httpClient.execute(httpGet);
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 (!httpGet.isAborted()) {
httpGet.abort();
}
httpClient.getConnectionManager().shutdown();
}
}
}
//servlet配置
<servlet>
<servlet-name>HttpClientGetProxyServlet</servlet-name>
<servlet-class>com.abin.lee.servlet.HttpClientGetProxyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HttpClientGetProxyServlet</servlet-name>
<url-pattern>/HttpClientGetProxyServlet</url-pattern>
</servlet-mapping>
我这里的IP和端口不一定能用哟,自己找能用的!!代码是100%没问题的,经过生产环境测试的哟!!!