Java 代理服务器

一、需求

通过JAVA实现一个代理服务器,客户端的Http请求全部指向代理服务器,代理服务器基于一定的策略将请求转发给后台服务器。

二、需求分析

需求可以简化为 
        1、代理服务主机将客户机与代理服务主机之间的请求截获,然后直接将信息转发给目标主机 

 

        2、代理服务主机将目标主机的回应直接转发给客户机。

 

 

        代理服务主机可以通过socket直接获取客户机发送到本机指定端口的数据报文,那么通过分析内容可以得到客户机想要发送给目标主机的信息,代理服务主机可以通过socket的方式再次重新请求,将原来请求中的内容转发给目标主机,之后将目标主机的回应转发给客户机。

对于Java SE项目来说,有两者方式来实现:

        1、采用原生的Java socket 编程。(存在多线程问题...)

        2、采用第三方的网络应用框架,比如:mina、netty等。(开发简单,稳定可靠)

对于Java WEB项目来说,也有两者方式来实现:

        1、采用上面的Java SE方案。

        2、HTTP代理,拦截客户端的请求,并采用Http协议发送GET/POST请求给目标服务器。(这里又可以分三种种情况)

三、Demo实例

    1、原先的socket代理百度上一大堆...

 

    2、mina代理demo

见:http://svn.apache.org/repos/asf/mina/mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/proxy/
只需要自己写客户机和目标服务器了。

 

    3、netty代理demo

        见:https://download.csdn.net/download/wqc19920906/10270714

    4、Http代理服务器demo

        发送http请求分别有

        1)、用HttpURLConnection发送请求

        2)、使用URLConnection发送请求

        2)、使用HttpClient发送请求

        4)、使用socket发送请求 

        详见:https://blog.csdn.net/u010197591/article/details/51441399

四、自己用的HttpClient代理。

 

基本原理: 代理服务器打开一个端口接收浏览器发来的访问某个站点的请求,从请求的字符串中解析出用户想访问哪个网页,让后通过URL对象建立输入流读取相应的网页内容,最后按照web服务器的工作方式将网页内容发送给用户浏览器 源程序: import java.net.*; import java.io.*; public class MyProxyServer { public static void main(String args[]) { try { ServerSocket ss=new ServerSocket(8080); System.out.println("proxy server OK"); while (true) { Socket s=ss.accept(); process p=new process(s); Thread t=new Thread(p); t.start(); } } catch (Exception e) { System.out.println(e); } } }; class process implements Runnable { Socket s; public process(Socket s1) { s=s1; } public void run() { String content=" "; try { PrintStream out=new PrintStream(s.getOutputStream()); BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream())); String info=in.readLine(); System.out.println("now got "+info); int sp1=info.indexOf(' '); int sp2=info.indexOf(' ',sp1+1); String gotourl=info.substring(sp1,sp2); System.out.println("now connecting "+gotourl); URL con=new URL(gotourl); InputStream gotoin=con.openStream(); int n=gotoin.available(); byte buf[]=new byte[1024]; out.println("HTTP/1.0 200 OK"); out.println("MIME_version:1.0"); out.println("Content_Type:text/html"); out.println("Content_Length:"+n); out.println(" "); while ((n=gotoin.read(buf))>=0) { out.write(buf,0,n); } out.close(); s.close(); } catch (IOException e) { System.out.println("Exception:"+e); } } };
以下是一个简单的 Java 代理服务器代码示例: ```java import java.net.*; public class ProxyServer { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(8080); while (true) { Socket clientSocket = serverSocket.accept(); Thread thread = new Thread(new ProxyThread(clientSocket)); thread.start(); } } } class ProxyThread implements Runnable { private Socket clientSocket; public ProxyThread(Socket clientSocket) { this.clientSocket = clientSocket; } public void run() { try { // 读取客户端请求 BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine; String url = ""; while ((inputLine = in.readLine()) != null) { if (inputLine.startsWith("GET ")) { url = inputLine.substring(4, inputLine.indexOf(" ", 4)); } } // 发送请求到目标服务器 URL targetUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) targetUrl.openConnection(); connection.setRequestMethod("GET"); // 将目标服务器响应转发给客户端 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String responseLine; OutputStreamWriter out = new OutputStreamWriter(clientSocket.getOutputStream()); while ((responseLine = reader.readLine()) != null) { out.write(responseLine + "\r\n"); } out.flush(); // 关闭连接 reader.close(); in.close(); out.close(); clientSocket.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这个代理服务器监听 8080 端口,接受客户端请求并将请求转发到目标服务器。在这个示例中,我们只处理 HTTP GET 请求,并将目标服务器响应直接转发给客户端。你可以根据需要修改代码以处理其他类型的请求和响应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值