当前位置: 技术问答>java相关
如何建立通过代理来访问的URL?
来源: 互联网 发布时间:2015-04-17
本文导语: 我访问WEB要通过代理服务器 172.20.5.5:8080. 在程序中,一般 URL aURL = new URL("http://www.sina.com.cn"); URLConnection connection=url.openConnection(); connection.connect(); 即可。 但我要通过代理服务器的话,是不时还有其它的设置。 ...
我访问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) {
}
...
请大家指正。谢谢
在程序中,一般
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) {
}
...
请大家指正。谢谢
|
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();
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();
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。