设为首页
加入收藏
发布文章
  首页   操作系统   系统集成   程序设计   网站设计   图形图象   安全技术   反击技术   硬件DIY   认证考试   免费资源
当前位置:中国E盟 -=> 程序设计 -=> 网络编程 -=> jsp -=> 正文  
java程序如何穿透带有密码验证的代理 << JSP中读文件和写文件 | 使用JSP开发WebMail系统 >> 
 发布日期:2006-6-20 12:59:06 发布者:[中国E盟]  来源:[本站]  浏览:[]  评论:[ 字体:   

use inner instead of extends is the most different

网上也有一些文章但是大多数涉及带有授权验证的proxy都有问题,
主要问题就是出在对 Authenticator.setDefault的使用,以及base64编码的问题上
代码是最没有二义性的文档,实现原理不再解释,请看代码去体会。
如果转载请注明代码出处。

[email]chimae@cnjsp.org[/email]

package org.chimae.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;

/**
* @author [email]chimae@cnjsp.org[/email]
*/
public class ProxyConnTest {
   
    public static void initProxy(String host, int port, final String username,
            final String password) {
        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username,
                        new String(password).toCharArray());
            }
        });

   
        System.setProperty(\"http.proxyType\", \"4\");
        System.setProperty(\"http.proxyPort\", Integer.toString(port));
        System.setProperty(\"http.proxyHost\", host);
        System.setProperty(\"http.proxySet\", \"true\");
    }
   
   
    public static void main(String[] args) throws IOException {
        String url = \"http://java.sun.com/\";
        String proxy = \"yourProxy\";
        int port =8080;
        String username =\"username\";
        String password =\"password\";
        String curLine = \"\";
        String content = \"\";        
        URL server = new URL(url);
        initProxy(proxy,port,username,password);
        HttpURLConnection connection = (HttpURLConnection)server.openConnection();
        connection.connect();
        InputStream is = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        while ((curLine = reader.readLine()) != null) {
                content += curLine;
            }

        System.out.println(\"content= \" + content);
        is.close();
    }

} ]]>
打印 】【 收藏 】【 推荐 】 
相关文章 推荐文章 热门文章
XML到Java代码的数据绑定之..[2.24]
 ·暂无
初学jsp心得
JSP中读文件和写文件
用JSP编写文件上传
jsp计数器制作手册
通过JDBC连接oracle数据库的十..
 评一评
正在读取…
  笔名:
  评论:
   
【注】 发表评论必需遵守以下条例:
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款

设置首页 - 加入收藏 - 关于我们 - 联系我们 - 友情连接 - -

Copyright ©2006-2008 中国E盟 Powered By:www.chinaE.org

湘ICP备05004075号