欢迎光临
我们一直在努力

java程序如何穿透带有密码验证的代理-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

网上也有一些文章但是大多数涉及带有授权验证的proxy都有问题,

主要问题就是出在对 authenticator.setdefault的使用,以及base64编码的问题上

代码是最没有二义性的文档,实现原理不再解释,请看代码去体会。

如果转载请注明代码出处。

chimae@cnjsp.org

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 chimae@cnjsp.org

 */

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();

    }

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » java程序如何穿透带有密码验证的代理-JSP教程,Java技巧及代码
分享到: 更多 (0)