JAVA实现HTTP代理上网的测试程序
2009-09-03 17:11
453 查看
一、实验环境:
编程语言:Java1.5(运行在JVM(Java Virsual Machine))
开发工具:eclipce3.2
测试环境:局域网
二、需求分析:
随着Internet技术的迅速发展,越来越多的计算机连入了Internet,出现了IP地址不足的现象.在这种情况下,代理服务器便应运而生了.
三、HTTP Proxy的实现代码:
/**
* 本程序实现简单的HTTP代理客户端浏览器
* 步骤:
* 1、调用java.net包中的URL类并生成对象url;
* 2、调用url.openConnection()方法实现直接连接;
* 3、调用url.openConnection(Proxy)方法实现代理连接;
* 4、Proxy(Proxy.Type.HTTP, proxyAddress)实现HTTP代理的对象;
* 5、文件浏览功能的实现原理:将需要浏览的文件下载到本机,然后读取出来并显示在面板上;
* 作者:蓝蔡
*/
---------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ProxyTest {
@SuppressWarnings("deprecation")
public static void main(String[] args)
{
JFrame proxyFrame =new JFrame("代理客户端");
final JTextArea showArea =new JTextArea(10,20);
JPanel northPanel =new JPanel(new FlowLayout());
JScrollPane centerPanel =new JScrollPane(showArea);
JPanel southPanel =new JPanel(new FlowLayout());
JLabel addressLabel =new JLabel("输入网址:");
JLabel selectLabel =new JLabel("选择代理类型:");
JLabel proxyLabel =new JLabel("代理");
JLabel noProxyLabel =new JLabel("直连");
JButton showButton =new JButton("浏览");
final JRadioButton proxyRadioButton =new JRadioButton();
final JRadioButton noProxyRadioButton =new JRadioButton();
final JTextField addressField =new JTextField(15);
proxyFrame.setSize(350, 350);
proxyFrame.setLocation(150, 150);
proxyFrame.setLayout(new BorderLayout());
showArea.setWrapStyleWord(true);
// NorthPanel
northPanel.add(addressLabel);
northPanel.add(addressField);
northPanel.add(showButton);
proxyFrame.getContentPane().add(northPanel,"North");
// CenterPanel
// proxyFrame.getContentPane().add(centerPanel,BorderLayout.CENTER);
// SouthPanel
southPanel.add(selectLabel);
southPanel.add(proxyRadioButton);
southPanel.add(proxyLabel);
southPanel.add(noProxyRadioButton);
southPanel.add(noProxyLabel);
proxyFrame.getContentPane().add(southPanel,"South");
proxyFrame.setDefaultCloseOperation(proxyFrame.EXIT_ON_CLOSE);
proxyFrame.show();
// 点击“浏览”按钮的事件响应代码
showButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str =addressField.getText();
URL url=null;
URLConnection urlCon=null;
try
{
showArea.setText("");
// 直接连接
if(!(proxyRadioButton.isSelected())&&(noProxyRadioButton.isSelected()) )
{
url=new URL(str);
urlCon=url.openConnection();
}
// 代理连接默认为HTTP
else if ((proxyRadioButton.isSelected())&&!(noProxyRadioButton.isSelected()) ){
SocketAddress proxyAddress=new InetSocketAddress("192.168.100.76",8084);
Proxy proxy=new Proxy(Proxy.Type.HTTP, proxyAddress);
url=new URL(str);
urlCon=url.openConnection(proxy);
}else
JOptionPane.showMessageDialog(null, "请选择连接类型");
// 以下显示连接的属性:主机名,端口,连接的文件类型,连接的文件长度
final String line =System.getProperty("line.separator");
showArea.append("host :"+url.getHost());
showArea.append(line);
showArea.append("port :"+url.getPort());
showArea.append(line);
showArea.append("Contenttype :"+urlCon.getContentType());
showArea.append(line);
showArea.append("Contentlength :"+urlCon.getContentLength());
final InputStream in=urlCon.getInputStream();
final FileOutputStream out=new FileOutputStream("Temp.txt");
final FileInputStream display=new FileInputStream("Temp.txt");
// 利用文件下载和读取的方式实现浏览功能
// 此处利用线程实现是为了实现多个任务的并发进行
Runnable r=new Runnable(){
int data;
byte[] data2=new byte[10*1024];
int len;
// 此处利用实现synchronized同步,避免不同线程之间冲突
synchronized public void run(){
try{
while((data=in.read())!=-1)
out.write(data);
out.close();
in.close();
len=display.read(data2);
display.close();
showArea.append(new String(data2,0,len));
}catch(Exception ex){
System.out.println("读失败");
}
}
};
Thread t=new Thread(r);
t.start();
}catch(Exception ex){
ex.getStackTrace();
}
}
});
}
}
---------------------------------------------------
编程语言:Java1.5(运行在JVM(Java Virsual Machine))
开发工具:eclipce3.2
测试环境:局域网
二、需求分析:
随着Internet技术的迅速发展,越来越多的计算机连入了Internet,出现了IP地址不足的现象.在这种情况下,代理服务器便应运而生了.
三、HTTP Proxy的实现代码:
/**
* 本程序实现简单的HTTP代理客户端浏览器
* 步骤:
* 1、调用java.net包中的URL类并生成对象url;
* 2、调用url.openConnection()方法实现直接连接;
* 3、调用url.openConnection(Proxy)方法实现代理连接;
* 4、Proxy(Proxy.Type.HTTP, proxyAddress)实现HTTP代理的对象;
* 5、文件浏览功能的实现原理:将需要浏览的文件下载到本机,然后读取出来并显示在面板上;
* 作者:蓝蔡
*/
---------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ProxyTest {
@SuppressWarnings("deprecation")
public static void main(String[] args)
{
JFrame proxyFrame =new JFrame("代理客户端");
final JTextArea showArea =new JTextArea(10,20);
JPanel northPanel =new JPanel(new FlowLayout());
JScrollPane centerPanel =new JScrollPane(showArea);
JPanel southPanel =new JPanel(new FlowLayout());
JLabel addressLabel =new JLabel("输入网址:");
JLabel selectLabel =new JLabel("选择代理类型:");
JLabel proxyLabel =new JLabel("代理");
JLabel noProxyLabel =new JLabel("直连");
JButton showButton =new JButton("浏览");
final JRadioButton proxyRadioButton =new JRadioButton();
final JRadioButton noProxyRadioButton =new JRadioButton();
final JTextField addressField =new JTextField(15);
proxyFrame.setSize(350, 350);
proxyFrame.setLocation(150, 150);
proxyFrame.setLayout(new BorderLayout());
showArea.setWrapStyleWord(true);
// NorthPanel
northPanel.add(addressLabel);
northPanel.add(addressField);
northPanel.add(showButton);
proxyFrame.getContentPane().add(northPanel,"North");
// CenterPanel
// proxyFrame.getContentPane().add(centerPanel,BorderLayout.CENTER);
// SouthPanel
southPanel.add(selectLabel);
southPanel.add(proxyRadioButton);
southPanel.add(proxyLabel);
southPanel.add(noProxyRadioButton);
southPanel.add(noProxyLabel);
proxyFrame.getContentPane().add(southPanel,"South");
proxyFrame.setDefaultCloseOperation(proxyFrame.EXIT_ON_CLOSE);
proxyFrame.show();
// 点击“浏览”按钮的事件响应代码
showButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str =addressField.getText();
URL url=null;
URLConnection urlCon=null;
try
{
showArea.setText("");
// 直接连接
if(!(proxyRadioButton.isSelected())&&(noProxyRadioButton.isSelected()) )
{
url=new URL(str);
urlCon=url.openConnection();
}
// 代理连接默认为HTTP
else if ((proxyRadioButton.isSelected())&&!(noProxyRadioButton.isSelected()) ){
SocketAddress proxyAddress=new InetSocketAddress("192.168.100.76",8084);
Proxy proxy=new Proxy(Proxy.Type.HTTP, proxyAddress);
url=new URL(str);
urlCon=url.openConnection(proxy);
}else
JOptionPane.showMessageDialog(null, "请选择连接类型");
// 以下显示连接的属性:主机名,端口,连接的文件类型,连接的文件长度
final String line =System.getProperty("line.separator");
showArea.append("host :"+url.getHost());
showArea.append(line);
showArea.append("port :"+url.getPort());
showArea.append(line);
showArea.append("Contenttype :"+urlCon.getContentType());
showArea.append(line);
showArea.append("Contentlength :"+urlCon.getContentLength());
final InputStream in=urlCon.getInputStream();
final FileOutputStream out=new FileOutputStream("Temp.txt");
final FileInputStream display=new FileInputStream("Temp.txt");
// 利用文件下载和读取的方式实现浏览功能
// 此处利用线程实现是为了实现多个任务的并发进行
Runnable r=new Runnable(){
int data;
byte[] data2=new byte[10*1024];
int len;
// 此处利用实现synchronized同步,避免不同线程之间冲突
synchronized public void run(){
try{
while((data=in.read())!=-1)
out.write(data);
out.close();
in.close();
len=display.read(data2);
display.close();
showArea.append(new String(data2,0,len));
}catch(Exception ex){
System.out.println("读失败");
}
}
};
Thread t=new Thread(r);
t.start();
}catch(Exception ex){
ex.getStackTrace();
}
}
});
}
}
---------------------------------------------------
相关文章推荐
- Java测试程序运行时间 实现
- Java实训——创建一个窗体程序,实现简单的数学加、减法测试。
- Java实现的闰年测试程序
- 一个Java实现的简单的多个客户端聊天程序(未测试)
- 相对准确的java程序性能测试实现方案
- 用Java实现的简单人品测试程序
- java简单实现测试程序执行时间及日期转换字符串
- 电话薄Java实现小程序
- Java实现算法导论中Miller-Rabin随机性素数测试
- 使用Java程序来实现HTTP文件的队列下载
- java实现微信小程序支付
- 简单Java图书管理程序(只实现了界面的显示)
- 实现Java程序跨平台运行的注意事项
- Java多线程实现快速切分文件的程序
- Java实现对称解密小程序用户信息(AES-128-CBC算法)
- java程序实现线程连接池功能
- java用程序实现subString功能
- Linux下搭建Java环境变量并实现第一个Java小程序
- 经典java转码程序,实现和 jdk\bin\native2ascii.exe 同样的功能
- 关于用Java程序实现8421码权限管理 -第三部分