java 设置代理访问http

 

java 的System.getProperties()方法2009-07-14 14:501、java 通过System.getProperties()获取系统参数

Properties props=System.getProperties(); //系统属性
System.out.println("Java的运行环境版本:"+props.getProperty("java.version"));
System.out.println("Java的运行环境供应商:"+props.getProperty("java.vendor"));
System.out.println("Java供应商的URL:"+props.getProperty("java.vendor.url"));
System.out.println("Java的安装路径:"+props.getProperty("java.home"));
System.out.println("Java的虚拟机规范版本:"+props.getProperty("java.vm.specification.version"));
System.out.println("Java的虚拟机规范供应商:"+props.getProperty("java.vm.specification.vendor"));
System.out.println("Java的虚拟机规范名称:"+props.getProperty("java.vm.specification.name"));
System.out.println("Java的虚拟机实现版本:"+props.getProperty("java.vm.version"));
System.out.println("Java的虚拟机实现供应商:"+props.getProperty("java.vm.vendor"));
System.out.println("Java的虚拟机实现名称:"+props.getProperty("java.vm.name"));
System.out.println("Java运行时环境规范版本:"+props.getProperty("java.specification.version"));
System.out.println("Java运行时环境规范供应商:"+props.getProperty("java.specification.vender"));
System.out.println("Java运行时环境规范名称:"+props.getProperty("java.specification.name"));
System.out.println("Java的类格式版本号:"+props.getProperty("java.class.version"));
System.out.println("Java的类路径:"+props.getProperty("java.class.path"));
System.out.println("加载库时搜索的路径列表:"+props.getProperty("java.library.path"));
System.out.println("默认的临时文件路径:"+props.getProperty("java.io.tmpdir"));
System.out.println("一个或多个扩展目录的路径:"+props.getProperty("java.ext.dirs"));
System.out.println("操作系统的名称:"+props.getProperty("os.name"));
System.out.println("操作系统的构架:"+props.getProperty("os.arch"));
System.out.println("操作系统的版本:"+props.getProperty("os.version"));
System.out.println("文件分隔符:"+props.getProperty("file.separator")); //在 unix 系统中是"/"
System.out.println("路径分隔符:"+props.getProperty("path.separator")); //在 unix 系统中是":"
System.out.println("行分隔符:"+props.getProperty("line.separator")); //在 unix 系统中是"/n"
System.out.println("用户的账户名称:"+props.getProperty("user.name"));
System.out.println("用户的主目录:"+props.getProperty("user.home"));
System.out.println("用户的当前工作目录:"+props.getProperty("user.dir"));

 

2、用Java编写通过代理访问的应用程序

本技巧将向您讲述如何编写可通过代理访问因特网上的Web服务器的Java应用程序。在Java应用程序中加入代理支持只需额外编写几行代码,且不依赖任何安全性“漏洞”。

将Java和代理结合起来的秘诀即在Java运行时激活特定的系统属性。这些属性未被写入正式文件,只是作为Java传说的一部分在Java编程人员中秘传。为了支持代理,Java应用程序不仅需要指定代理本身的信息,而且需要指定用于认证的用户信息。在开始使用网际协议之前,您需要在程序中添加以下几行代码:
//通知Java您要通过代理进行连接
System.getProperties().put("proxySet","true");

//指定代理所在的服务器
System.getProperties().put("proxyHost","myProxyMachineName");

//指定代理监听的端口
System.getProperties().put("proxyPort","85");

 有些代理在授权用户访问因特网之前,要求用户输入用户名和口令。如果您使用位于防火墙之内的Web浏览器,您就可能碰到过这种情况。以下是执行认证的方法:

URLConnection connection=url.openConnection();
String password="username:password";
String encodedPassword=base64Encode(password);
connection.setRequestProperty("Proxy-Authorization",encodedPassword);

  这段代码的思想是,您必须调整HTTP标头以发出用户信息。这是通过调用setRequestProperty()来实现的。这种方法允许您在发出请求之前处理HTTP标头。HTTP要求用base64对用户名和口令进行编码。幸运的是,有一组公用域API,它们将代您执行编码(请参阅参考资源部分)。

  如您所见,在Java应用程序中加入代理支持并不需要做多少工作。有了现在的知识,再做一点研究(您必须查明您的代理是如何处理您感兴趣的协议以及如何进行用户认证的),您就能用其他协议实现代理。
HTTP代理:(例子)

Properties props = System.getProperties();

props.put("http.proxyHost", "192.168.0.150");

props.put("http.proxyPort", "808");
  FTP代理

  ScottD.Taylor提出这个秘诀来处理FTP协议代理:

defaultProperties.put("ftpProxySet","true");
defaultProperties.put("ftpProxyHost","proxy-host-name");
defaultProperties.put("ftpProxyPort","85");

  接下来您便可以通过以下代码使用"ftp"协议访问文件URL:

URL url=newURL("ftp://ftp.netscape.com/pub/navigator/3.04/windows/readme.txt");

  如果有人有使用其他网际协议代理的例子,我很想看看。

  注意:代码示例(Example.java)仅在JDK1.1.4下测试过。

  后续技巧!

  对于仍在使用JDK1.1.7(配合WebSphere3.0)的开发人员而言,将proxyHost和proxyPort设为系统属性不起作用;conn.getInputStream()或者返回连接超时,或者是找不到主机路径。但是,我使用接受Host和Port为参数的URL构造函数解决了这一问题(使用我的代理主机和端口):

public URL(String protocol,String host,int port,String file).

  借助用户名和口令进行认证的方法不起作用。应将"Basic"置于认证字符串的开头;例如:

String encodedPassword=base64Encode(password);

  应该是:

String encodedPassword="Basic"+base64Encode(password);

  您也不必用一个单独的程序来进行64位编码。您可以使用sun.misc.BASE64Encoder()类。下面是完成这两处改动之后的代码:

System.getProperties().put("proxySet","true");
System.getProperties().put("proxyHost",proxyHost);
System.getProperties().put("proxyPort",proxyPort);
String authString="userid:password";
String auth="Basic"+newsun.misc.BASE64Encoder().encode(authString.getBytes());
URL url=newURL("http://java.sun.com/");
URLConnection conn=url.openConnection();
conn.setRequestProperty("Proxy-Authorization",auth);

  下面是使用socks4代理服务器的方法:

System.getProperty("socksProxySet",true);
System.getProperty("socksProxyHost",proxyHostName);
System.getProperty("socksProxyPort",proxyPort);
Usually the proxyPort for Socks 4 is port 1080


java附加http代理例子:

public static void main(String[] args) throws Exception {
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", "192.168.1.253");
System.getProperties().put("proxyPort", "8080");
Authenticator.setDefault(new MyAuthenticator("YH714737","ps516228"));
FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache);

URL url = new URL("http://www.china-pub.com/rss/FeedPub.asp?id=3");
//url是XML文件, 如http://feed.feedsky.com/xxx.xml
SyndFeed feed = feedFetcher.retrieveFeed(url);
List entryList = feed.getEntries();
for (int i = 0; i < entryList.size(); i++) {
SyndEntry entry = (SyndEntry) entryList.get(i);
System.out.println("Published Date: " + entry.getPublishedDate());
System.out.println("Title: " + entry.getTitle());
System.out.println("Link: " + entry.getLink());
SyndContent sc = entry.getDescription();
if(sc!=null){
System.out.println("Description: " + sc.getValue());
}
System.out.println("------------------------------");
}

}

 

=================================================

package org.slave4j.demo.blog.action;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;


public class GetInternet {

/**
* 获得网页中的超链接
* @author letthinking
* @param urlStr url 例如:http://blog.csdn.net/yue19870813?viewmode=list
* @return List<String>
*/
public List<String> getInternet(String urlStr){
List<String> list = new ArrayList<String>();
URL url = null;
URLConnection conn = null;
String nextLine = null;
StringTokenizer tokenizer = null;
//通知Java您要通过代理进行连接
System.getProperties().put("proxySet","true");
//指定代理所在的服务器
System.getProperties().put("proxyHost","192.168.50.243");
//指定代理监听的端口
System.getProperties().put("proxyPort","10080");

try{
//获得网页资源
url = new URL(urlStr);
//获得资源连接
conn = url.openConnection();
conn.connect();
BufferedReader reader = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
//开始读取网页信息解析出网页中的超链接
while((nextLine = reader.readLine()) != null ){
tokenizer = new StringTokenizer(nextLine);
while(tokenizer.hasMoreTokens()){
String urlToken = tokenizer.nextToken();
if(isUrl(urlToken)){
list.add(getHttp(urlToken));
}
}
}

}catch(Exception e){

}
return list;
}

/**
* 判断字符串中是否含有超链接
* @author letthinking
* @param urlToken
* @return
*/
public boolean isUrl(String urlToken){
if(urlToken.indexOf("http") != -1){
return true;
}
return false;
}

/**
* 将字符串中超链接提取出来
* @author letthinking
* @param urlToken
* @return
*/
public String getHttp(String urlToken){
int start = urlToken.indexOf("http");
int end = urlToken.length();
String tempStr = urlToken.substring(start,end);
end = tempStr.indexOf("\"");
if(end == -1){
end = tempStr.length();
}
return tempStr.substring(0,end);
}

public static void main(String[] args){
GetInternet g = new GetInternet();
List<String> list = g.getInternet("http://www.baidu.com");
System.out.println("开始输出超链接");
for(String str:list){
System.out.println(str);
}
//System.out.println(g.getHttp("<link rel=\"stylesheet\" href=\"http://csdnimg.cn/www/css/main_new.css?20110813\" type=\"text/css\" media=\"all\" />"));
}
}

 

上面这个例子可以通过代理抓取制定页面的信息

 

posted on 2013-05-15 12:54  淇水na个弯  阅读(1257)  评论(0编辑  收藏  举报

导航