[code="java"]package src.test;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import sun.misc.BASE64Encoder;
public class URLTest {
public static void main(String[] args){
try{
System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", "11.0.1.210");
System.setProperty("http.proxyPort", "8080");
URL u = new URL("http://www.baidu.com");
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
String authentication = "caixian2:96191264";
String encodedLogin = new BASE64Encoder().encodeBuffer(authentication.getBytes());
conn.setRequestProperty("Proxy-Authorization", "Basic " + encodedLogin);
conn.connect();
int length = conn.getContentLength();
System.out.println(length);
InputStream is = conn.getInputStream();
byte[] b = new byte[4 * 1024];
is.read(b);
for(int i = 0; i < b.length; i++){
System.out.print((char)b[i]);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
[/code]
我在单位的内部网通过代理设置上网,现在想写个程序来访问外部网,在网上找了哈偶都资料,我个人感觉代码应该没有错误啊,可是就是报异常,如下:
[code="java"]java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic Y2FpeGlhbjI6OTYxOTEyNjQ=
at sun.net.www.protocol.http.HttpURLConnection.checkMessageHeader(HttpURLConnection.java:200)
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:1553)
at src.test.URLTest.main(URLTest.java:23)
Exception in thread "main" [/code]
高人们帮个忙,看看我哪里还写错了,非常感谢!!!