.net 创建网站项目,网站开发建设推荐用书,个人网页设计硬件需求,苏州城乡建设局网站质检员大家都知道大名鼎鼎的BurpSuite代理神器#xff0c;对于抓取HTTP请求非常好用#xff0c;偶然#xff0c;一朋友问我Java应该如何去编写代理服务器#xff08;因为他想做某些东西#xff09;#xff0c;有没有相关的API 去实现#xff0c;我想说#xff0c;差不多你能想… 大家都知道大名鼎鼎的BurpSuite代理神器对于抓取HTTP请求非常好用偶然一朋友问我Java应该如何去编写代理服务器因为他想做某些东西有没有相关的API 去实现我想说差不多你能想到的JAVA都可以做到没有任何一门成熟的语言是垃圾的。 在编写代理服务器之前首先应该明白一点Java的代理机制如图1-1所示。 那么Java就处于中间这层代理服务器代理服务器所作的事情如下 1、接收客户端请求进行处理然后发送给服务端 2、接收服务端响应进行处理然后发送给客户端 这样就更清晰了Java给我们提供了代理的API为java.net.Proxy类。此类表示代理设置通常为类型http、socks和套接字地址。Proxy 是不可变对象。 也就是说Java可以制作高级协议的代理如 HTTP 或 FTP。也可以制作SOCKSV4 或 V5代理。 在基本的概念说完之后来实际操作一把分为两个步骤第一部分让JAVA程序使用代理服务器第二步部分让我们的Java程序像BurpSuite一样来做一个HTTP的代理服务器吧。 首先使用到了URL类HttpURLConnection类及其我们的代理类Proxy类。他们都位于java.net包中。 第一步生成代理指定端口为8888
Proxy proxy null ;
proxy new Proxy(Proxy.Type.HTTP,new InetSocketAddress(127.0.0.1,8888)); // 实例化本地代理对象端口为8888 第二步使用URLConnection类进行连接www.moonsos.com
URL url new URL(http://www.moonsos.com); //实例化米安网URL类
HttpURLConnection action (HttpURLConnection)url.openConnection(proxy); //使用代理打开网页 第三步打开URL并且读取HTML源码
HttpURLConnection action (HttpURLConnection)url.openConnection(proxy); //使用代理打开网页
InputStream in action.getInputStream();
BufferedReader br new BufferedReader(new InputStreamReader(in,UTF-8));
StringBuilder sb new StringBuilder();
String lin System.getProperty(line.separator) ;
for(String temp br.readLine() ; temp!null;temp br.readLine() ){
sb.append(templin);
}
br.close();
in.close();
System.out.println(sb); 效果执行图如图1-2所示。 完整代码示例如下
import java.net.* ;
import java.io.* ;
public class ProxyTest{
public static void main(String args[])throws Exception{
Proxy proxy null ;
proxy new Proxy(Proxy.Type.HTTP,new InetSocketAddress(127.0.0.1,8888)); // 实例化本地代理对象端口为8888
URL url new URL(http://www.moonsos.com);
HttpURLConnection action (HttpURLConnection)url.openConnection(proxy); //使用代理打开网页
InputStream in action.getInputStream();
BufferedReader br new BufferedReader(new InputStreamReader(in,UTF-8));
StringBuilder sb new StringBuilder();
String lin System.getProperty(line.separator) ;
for(String temp br.readLine() ; temp!null;temp br.readLine() ){
sb.append(templin);
}
br.close();
in.close();
System.out.println(sb);
}
} 第一部分我们学会了Java如何使用代理程序那么第二部分就看Java制作代理服务器。 第一步生成Socket类作为代理服务器
ServerSocket server new ServerSocket(8888); //建立本地代理服务器端口为8888 第二步等待连接也就是等待使用代理程序的用户进入如果没有用户进入那么将会一直在此等待。 Socket socket server. accept(); //等待客户端连接 第三步当用户进来后查看用户数据发送的请求这里新做了一个ActionScoket类多线程专门用来处理Scoket输入流代码如下所所示。
ServerSocket server new ServerSocket(8888);
while(true){
Socket socket server.accept();
ActionSocket ap new ActionSocket(socket);
ap.start();
}
ActionSocket代码如下
class ActionSocket extends Thread{
private Socket socket null ;
public ActionSocket(Socket s){
this.socket s ;
}
public void run(){
try{
this.action() ;
}catch(Exception e){
e.printStackTrace();
}
}
public void action() throws Exception {
if (this.socket null){
return ;
}
BufferedReader br new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
for(String temp br.readLine() ; temp!null;temp br.readLine() ){
System.out.println(temp);
}
br.close();
}
} 完成代码如下
import java.net.* ;
import java.io.* ;
class ActionSocket extends Thread{
private Socket socket null ;
public ActionSocket(Socket s){
this.socket s ;
}
public void run(){
try{
this.action() ;
}catch(Exception e){
e.printStackTrace();
}
}
public void action() throws Exception {
if (this.socket null){
return ;
}
BufferedReader br new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
for(String temp br.readLine() ; temp!null;temp br.readLine() ){
System.out.println(temp);
}
br.close();
}
}
public class ServerPrxoy{
public static void main(String args[])throws Exception{
ServerSocket server new ServerSocket(8888);
while(true){
Socket socket server.accept();
ActionSocket ap new ActionSocket(socket);
ap.start();
}
}
} 给火狐搜狗等浏览器配置代理如图1-3所示 OK配置完毕进行访问http://www.moonsos.com可以发现我们写的小程序已经能够进行抓取到HTTP协议信息如图1-4所示。 当获取HTTP请求之后我想后面的东西就不用说了吧。无非就是对HTTP请求进行分析封装。然后在时候Socket发送。获取到信息之后在使用当前的Socket以打印流的方式输出到浏览器。