代码如下 ,我想通过代理访问一些网址,但是不知道如何测试代理成功,我想通过访问“http://www.blogger.com/”来看看是否代理成功,但是也不能访问,出现异常“Unexpected end of file from server”,但是如果不这样测试,如何能确定代理访问成功了呢?
谢谢package wf.yjy.zhxxj;import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Properties;public class HttpProxy {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Properties prop = System.getProperties();
prop.put("proxySet","true");
// 设置http访问要使用的代理服务器的地址
prop.setProperty("http.proxyHost", "130.194.11.121");
// 设置http访问要使用的代理服务器的端口
prop.setProperty("http.proxyPort", "1080");
prop.list(System.out);
// 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
//prop.setProperty("http.nonProxyHosts", "localhost|192.168.0.*");
// 设置安全访问使用的代理服务器地址与端口
// 它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问
/*
prop.setProperty("https.proxyHost", "192.168.0.254");
prop.setProperty("https.proxyPort", "443");
*/
// 使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机
/*
prop.setProperty("ftp.proxyHost", "192.168.0.254");
prop.setProperty("ftp.proxyPort", "2121");
prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*");
*/
// socks代理服务器的地址与端口
/*
prop.setProperty("socksProxyHost", "192.168.0.254");
prop.setProperty("socksProxyPort", "8000");
*/
// 设置登陆到代理服务器的用户名和密码
Authenticator.setDefault(new MyAuthenticator("", ""));
URL url;
HttpURLConnection http;
InputStream urlstream;
try {
url = new URL("http://www.blogger.com/");
http = (HttpURLConnection)url.openConnection();
http.connect();
urlstream = http.getInputStream();
byte buffer[] = new byte[1024];
int i;
while ((i =urlstream.read(buffer)) != -1) {
System.out.write(buffer, 0, i);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class MyAuthenticator extends Authenticator {
private String user = "";
private String password = "";
public MyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
}
谢谢package wf.yjy.zhxxj;import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Properties;public class HttpProxy {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Properties prop = System.getProperties();
prop.put("proxySet","true");
// 设置http访问要使用的代理服务器的地址
prop.setProperty("http.proxyHost", "130.194.11.121");
// 设置http访问要使用的代理服务器的端口
prop.setProperty("http.proxyPort", "1080");
prop.list(System.out);
// 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
//prop.setProperty("http.nonProxyHosts", "localhost|192.168.0.*");
// 设置安全访问使用的代理服务器地址与端口
// 它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问
/*
prop.setProperty("https.proxyHost", "192.168.0.254");
prop.setProperty("https.proxyPort", "443");
*/
// 使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机
/*
prop.setProperty("ftp.proxyHost", "192.168.0.254");
prop.setProperty("ftp.proxyPort", "2121");
prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*");
*/
// socks代理服务器的地址与端口
/*
prop.setProperty("socksProxyHost", "192.168.0.254");
prop.setProperty("socksProxyPort", "8000");
*/
// 设置登陆到代理服务器的用户名和密码
Authenticator.setDefault(new MyAuthenticator("", ""));
URL url;
HttpURLConnection http;
InputStream urlstream;
try {
url = new URL("http://www.blogger.com/");
http = (HttpURLConnection)url.openConnection();
http.connect();
urlstream = http.getInputStream();
byte buffer[] = new byte[1024];
int i;
while ((i =urlstream.read(buffer)) != -1) {
System.out.write(buffer, 0, i);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class MyAuthenticator extends Authenticator {
private String user = "";
private String password = "";
public MyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
}
HTTP/1.0 200 OK // 表示服务器支持HTTP/1.0 协议,成功
HTTP/1.1 200 OK // 表示服务器支持HTTP/1.1 协议,成功
HTTP/1.0 404 Not Found //访问文件没有找到(好像跟局域网有关系)
要是500以上就错了。