基本原理:
代理服务器打开一个端口接收浏览器发来的访问某个站点的请求,从请求的字符串中解析出用户想访问哪个网页,让后通过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编写的代理服务器
共2个文件
url:1个
txt:1个
5星 · 超过95%的资源 需积分: 3 25 下载量 41 浏览量
2008-11-28
13:34:02
上传
评论
收藏 1KB RAR 举报
温馨提示
java编写的代理服务器 学习工作的好参考 大家可以参考下
资源推荐
资源详情
资源评论
收起资源包目录
.rar (2个子文件)
编写简单的代理服务器
200482505181
proxy.txt 2KB
龙腾教程网.url 245B
共 2 条
- 1
资源评论
- xinyewa2016-12-28可以参考,过于简单,有些响应头都是写死的
- Leslie_Vogel2013-06-20简洁明了,相当不错哦~~
- krist3332014-03-08相当不错,简洁明了。有一定参考价值
itheshe
- 粉丝: 2
- 资源: 58
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功