java 通过代理访问internet

本文介绍了一个使用Java实现的简单HTTP客户端程序,该程序通过设置代理服务器来访问网络资源,并详细展示了如何正确配置代理及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;



public class client {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		//  System.getProperties().setProperty("proxySet", "true"); 
		  System.getProperties().setProperty("http.proxyHost","172.19.4.42");
		  System.getProperties().setProperty("http.proxyPort","8080");
		   URL u = new URL("http://www.163.com");
		    URLConnection uc = u.openConnection();
		    String contentType = uc.getContentType();
		    int contentLength = uc.getContentLength();

		    InputStream raw = uc.getInputStream();
		    InputStream in = new BufferedInputStream(raw);
			int zimu=0;
			while((zimu=in.read())!=-1)
			{
	            
	             System.out.print((char)zimu);
	               
	                 
	                 try {
						Thread.sleep(1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					 
			
			}
			
	}
}

    这里需要注意的是,

 

                   System.getProperties().setProperty("http.proxyHost","172.19.4.42");

   System.getProperties().setProperty("http.proxyPort","8080");

 

这两句如果写成:

   System.getProperties().setProperty("HttpProxyHost","172.19.4.42");

 System.getProperties().setProperty("HttpPoxyPort","8080");

 

就错了。

 

                  我们通过抓包,可以发现虽然自己没有写NTLM校验过程,但是数据包显示,设置了代理,居然自己会进行NTLM校验,不晓得为什么,(猜测似乎Java的URL已经自己实现了NTLM校验)呵呵,不过也好,省去了我们好多麻烦。下面想写写socks代理,看看能不能越过公司防火墙,呵呵!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值