创建连接是HttpsURLConnection,怎么设置代理服务器?

okok165124 2009-08-03 04:47:01
网上搜了下,知道两种,sysytem.setProperty 和 HTTPConnection.setProxyServer

哪位大侠知道HttpsURLConnection这种创建方式的。


多谢了。
...全文
764 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
okok165124 2009-08-14
  • 打赏
  • 举报
回复
顶一下吧,我的jdk是1.4的,没有Proxy类~~!!没有 openConnection(Proxy proxy)

怎么实现啊~~~大侠们~!!!
okok165124 2009-08-11
  • 打赏
  • 举报
回复
感谢zl3450341

我没看懂您2楼的 方法1,方法2 是什么目的。测试代理吗??

3楼的方法2和我用的类似,我是直接 prop.setProperty("http.proxyHost", "192.168.1.111");

不过这样有个问题,就是说prop是一个全局变量,工程里面的业务要求,业务1使用代理,业务2不使用代理,这样的话,prop 会影响业务2,现在的方法是,在业务1的方法中尽量短的代码中套上 设置代理 和清除代理的方法,不过这种是怕几率很小的并发,会影响业务2.

现在是不知道有没有好办法设置代理 用的是 请求方式设置,而不是这种全局的。

why_java 2009-08-08
  • 打赏
  • 举报
回复
真没听过!!!
zl3450341 2009-08-08
  • 打赏
  • 举报
回复

/**
* @version 1.00 2001-11-01
* @author fightboy
*/

import java.io.*;
import java.net.*;

public class SocketTest
{
public static void main(String[] args)
{
long before;
long after;
long len = 0;

before=System.currentTimeMillis();
try
{
Socket s = new Socket("101.127.88.66",8080);
//代理的地址
BufferedReader in = new BufferedReader
(new InputStreamReader(s.getInputStream()));
PrintStream out = new PrintStream(s.getOutputStream(),true);
boolean more = true;
out.println("GET "+args[0]+" HTTP/1.0");
//注1:http/1.1会有点小问题,就是用了keep-alive之后,数人
//流不会返回null,使得下边循环无法结束
//注2:使用方法如例子:java SocketTest
//http://www.sina.com.cn/
out.println("Accept-Language: zh-cn");
out.println("User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
out.println("Host: 202.bbb.mmm.nnn");
//把这句话中冒号后的内容改为你自己的ip就可以了
out.println();
out.println();
while (more)
{
String line = in.readLine();
if (line == null) more = false;
else
{
//System.out.println(line);
len = len + line.length();
}
}
after=System.currentTimeMillis();
System.out.println("time cost: "+(after-before));
System.out.println("bytes get: "+len);
}
catch (IOException e)
{
System.out.println("Error" + e);
}
}
}

方法2是很简单的方法:
完成同样的功能:

import java.util.Properties;

import java.net.*;

import java.io.*;

public class TestHttpProxy {

public static void main(String[] args) {

String sUrl = "http://java.sun.com/index.html";


Properties prop = System.getProperties();

prop.put("http.proxyHost","192.168.1.111");

prop.put("http.proxyPort","80");


try{

URL su = new URL(sUrl);

System.out.println("url : " + su);

URLConnection uc = su.openConnection();

System.out.println("uc : " + uc);

InputStream is = su.openStream();

System.out.println("ic : " + is.read());

is.close();

System.out.println("ok");

} catch(Exception e) {

e.printStackTrace();

}

}

}

zl3450341 2009-08-08
  • 打赏
  • 举报
回复

java设置代理

package com.path.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;

public class TURLConnection {
public static void main(String args[]) throws Exception
{

//设置代理上外网
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", "172.31.170.14");
System.getProperties().put("proxyPort", "8080");

/*

如果需要验证用户

//Authenticator.setDefault(new MyAuthenticator());

*/
URL url=new URL("http://www.csdn.net");
URLConnection urlCon=url.openConnection();

/*
* 方法1,一次过读取所有信息
*/
BufferedInputStream bis=new BufferedInputStream(urlCon.getInputStream());
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("C:aa.htm"));

byte b[]=new byte[bis.available()];
bis.read(b);
bos.write(b);
bos.flush();

bis.close();
bos.close();


/*
* 方法2,一个个字节地读取
*/
// InputStream is=urlCon.getInputStream();
// FileOutputStream fos=new FileOutputStream("C:bb.htm");
// int tmp=0;
// while((tmp=is.read())!=-1)
// {
// fos.write(tmp);
// }
// fos.flush();
// fos.close();
// is.close();
}
}

package com.path.test;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

public class MyAuthenticator extends Authenticator {

private String name ;
private String password;

public MyAuthenticator() {
super();
// TODO Auto-generated constructor stub
}


public MyAuthenticator(String name, String password) {
super();
// TODO Auto-generated constructor stub
this.name = name;
this.password = password;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(this.getName(),this.getPassword().toCharArray());

}

}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧