当前位置: 技术问答>java相关
代理服务器的问题,很急
来源: 互联网 发布时间:2015-11-15
本文导语: 我做了一个小程序,有的网站需要通过代理才能访问,那个代理又有用户名和密码,现在我已经知道了用户名和密码,想在我的程序中加一段关于设置代理服务器的代码,因在ie里设置代理服务器,运行程序时,他总...
我做了一个小程序,有的网站需要通过代理才能访问,那个代理又有用户名和密码,现在我已经知道了用户名和密码,想在我的程序中加一段关于设置代理服务器的代码,因在ie里设置代理服务器,运行程序时,他总报我连接超时,可能是代理服务器有用户名和密码,需确认才能通过,我想在我的程序里设置代理服务器,并一并确认户名和密码,该怎么办,我用的是jbuilder5.
有这方面的源代码吗?最好给出。谢谢!
有这方面的源代码吗?最好给出。谢谢!
|
给你我写的一段代码,看是否对你有帮助。适用于jdk1.4版本。
import java.net.*;
import java.io.*;
public class TestHttpProxy
{
private String httpURL;
private URL destURL;
private HttpURLConnection urlConn;
private String request;
public void connectURL(){
try{
destURL = new URL(/tech-qa-java/httpURL/index.html);
urlConn = (HttpURLConnection)destURL.openConnection();
System.out.println("Print HeaderFile:");
int i=0;
while(urlConn.getHeaderField(i)!=null){
System.out.println(urlConn.getHeaderFieldKey(i)+" "+urlConn.getHeaderField(i));
i++;
}
System.out.println("method="+urlConn.getRequestMethod());
System.out.println(""+urlConn.getResponseCode());
}catch(Exception e){
System.out.println("Connect URL Error :"+"n"+e);
}
}
public void sendPostRequest(){
connectURL();
String authString = "user:password";//代理的用户名密码。
String auth ="Basic "+new sun.misc.BASE64Encoder().encode(authString.getBytes());
System.out.println("AuthString : "+auth);
urlConn.setRequestProperty( "Proxy-Authorization", auth );
request="flytsu=password"; //请求网页的用户名密码。
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setUseCaches(false);
urlConn.setAllowUserInteraction(true);
urlConn.setRequestProperty("Content-type","application/x-www-form-urlencoded");
urlConn.setRequestProperty("Content-length", ""+request.length());
try{
DataOutputStream outStream = new DataOutputStream(urlConn.getOutputStream());
outStream.writeBytes(request);
outStream.close();
}catch(Exception e){
System.out.println("Send Post Request Error :"+"n"+e);
}
}
public void sendGetRequest(){
connectURL();
}
public void doResponse(){
try{
DataInputStream inStream = new DataInputStream(urlConn.getInputStream());
int ch;
while ((ch = inStream.read()) >= 0) {
System.out.print((char) ch);
}
inStream.close();
}catch(Exception e){
System.out.println("Do Response Error"+"n"+e);
}
}
public static void main(String args[]){
System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "http.proxyHost", "202.118.6.85" );
System.getProperties().put( "http.proxyPort", "8080" );
TestHttpProxy thp = new TestHttpProxy();
thp.httpURL="http://www.csdn.net/member/logon.asp";
thp.sendPostRequest();
thp.doResponse();
}
}
import java.net.*;
import java.io.*;
public class TestHttpProxy
{
private String httpURL;
private URL destURL;
private HttpURLConnection urlConn;
private String request;
public void connectURL(){
try{
destURL = new URL(/tech-qa-java/httpURL/index.html);
urlConn = (HttpURLConnection)destURL.openConnection();
System.out.println("Print HeaderFile:");
int i=0;
while(urlConn.getHeaderField(i)!=null){
System.out.println(urlConn.getHeaderFieldKey(i)+" "+urlConn.getHeaderField(i));
i++;
}
System.out.println("method="+urlConn.getRequestMethod());
System.out.println(""+urlConn.getResponseCode());
}catch(Exception e){
System.out.println("Connect URL Error :"+"n"+e);
}
}
public void sendPostRequest(){
connectURL();
String authString = "user:password";//代理的用户名密码。
String auth ="Basic "+new sun.misc.BASE64Encoder().encode(authString.getBytes());
System.out.println("AuthString : "+auth);
urlConn.setRequestProperty( "Proxy-Authorization", auth );
request="flytsu=password"; //请求网页的用户名密码。
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setUseCaches(false);
urlConn.setAllowUserInteraction(true);
urlConn.setRequestProperty("Content-type","application/x-www-form-urlencoded");
urlConn.setRequestProperty("Content-length", ""+request.length());
try{
DataOutputStream outStream = new DataOutputStream(urlConn.getOutputStream());
outStream.writeBytes(request);
outStream.close();
}catch(Exception e){
System.out.println("Send Post Request Error :"+"n"+e);
}
}
public void sendGetRequest(){
connectURL();
}
public void doResponse(){
try{
DataInputStream inStream = new DataInputStream(urlConn.getInputStream());
int ch;
while ((ch = inStream.read()) >= 0) {
System.out.print((char) ch);
}
inStream.close();
}catch(Exception e){
System.out.println("Do Response Error"+"n"+e);
}
}
public static void main(String args[]){
System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "http.proxyHost", "202.118.6.85" );
System.getProperties().put( "http.proxyPort", "8080" );
TestHttpProxy thp = new TestHttpProxy();
thp.httpURL="http://www.csdn.net/member/logon.asp";
thp.sendPostRequest();
thp.doResponse();
}
}