如何建立通过代理来访问的URL?

myair 2001-09-27 04:42:41
我访问WEB要通过代理服务器 172.20.5.5:8080.

在程序中,一般
URL aURL = new URL("http://www.sina.com.cn");
URLConnection connection=url.openConnection();
connection.connect();
即可。

但我要通过代理服务器的话,是不时还有其它的设置。

否则的话,下载图片的时候,大于10k的都很难下。而文本文件没有多大的关系。

不过下载图片的和下载文本的有区别
//for graphics
DataInputStream in=new DataInputStream(connection.getInputStream());
DataOutputStream out1=new DataOutputStream(new FileOutputStream(downloadPath+filename));
while(in.available()!=0) {
out1.writeByte(in.readByte());

in.close();
out1.close();

//for text
BufferedReader in = new BufferedReader(new InputStreamReader(new DataInputStream(connection.getInputStream())));
PrintWriter out1= new PrintWriter(new BufferedWriter(new FileWriter(downloadPath+filename)));
while((line=in.readLine())!=null) {
}
...

请大家指正。谢谢

...全文
481 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
coolbitf 2002-03-15
  • 打赏
  • 举报
回复
帐号,密码,还有一个东西:“域”!!

怎么办??
skyyoung 2001-09-27
  • 打赏
  • 举报
回复
Connect via socket through a Proxy
You have to set the following properties : proxySet, proxyHost and proxyPort.



This can be done when starting the JVM for a JAVA application : java -DproxySet=true -DproxyHost=myProxyServer.come -DproxyPort=80 MyJavaApp



Or in your source : import java.util.Properties;
...

Properties systemSettings = System.getProperties();
systemSettings.put("proxySet", "true");
systemSettings.put("proxyHost", "myProxyServer.com");
systemSettings.put("proxyPort", "80");
System.setProperties(systemSettings);





--------------------------------------------------------------------------------
NOTE:
You might need to identify yourself to the proxy server. Use the HTTP property "Proxy-Authorization" with a username:password base64 encoded. Properties systemSettings = System.getProperties();
...
System.setProperties(systemSettings);

URL url=new URL("http://someserver/somepage");
URLConnection uc = url.openConnection ();
String encoded = new String(Base64.base64Encode(new String("username:password").getBytes()));
uc.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
uc.connect();


62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧