急求解通过代理服务器访问internet资源遇到的问题!
有些公司的http代理服务器需要身份验证,包括域名、用户名、密码等信息。
我使用如下方式,但是总不能成功。希望各位大哥指点:
(http代理服务器不需要身份验证的时候可以通过) 代码如下:
public class Post {
public Post() {
}
public static void main(String[] args) throws Exception{
Post post1 = new Post();
Properties prop = System.getProperties();
prop.put( "http.proxySet", "true" );
prop.put( "http.proxyHost", "10.1.2.13" );
prop.put( "http.proxyPort", "80" );
// prop.put( "http.auth.ntlm.domain", "wonders_files" );
prop.put( "http.proxyType", "4");
String urlstring = "http://www.sina.com.cn/";
String parameters = "name=value1&age=value2";
URL url = new URL(urlstring);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("Proxy-Authorization","Basic mydomain\\username:password");
conn.setRequestMethod("GET");
conn.connect(); // throws IOException
InputStream input = conn.getInputStream();
File file = new File("D:/420/sina.html");
FileOutputStream fos = new FileOutputStream(file);
byte[] b = new byte[8192];
int count = 0;
while(true){
if((count=input.read(b))==-1) break;
fos.write(b,0,count);
}
fos.close();
}
}