java通过代理访问网络

使用代理方式连接到网络

@Test
    public void t13(){
        String charset = "utf-8" ; 
        String proxyHost = "代理地址" ; 
        int proxyPort = 1234 ; //代理端口
        String proxyUrsername = "登陆代理服务器的用户名" ; 
        String proxyPassword = "登陆代理服务器的密码" ; 
        String urlStr = "http://www.baidu.com" ; 
        String ret = "" ; 
        InputStream is = null ;  
        try {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)) ;
            URL url = new URL(urlStr) ;
            HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy) ;
            BASE64Encoder encoder = new BASE64Encoder() ;
            String authorization = new String(encoder.encode(new String(proxyUrsername+":"+proxyPassword).getBytes()));
            connection.setRequestProperty("Proxy-Authorization", "Basic " + authorization);  
            connection.setDoInput(true); 
            connection.setRequestMethod("GET");
            connection.connect() ; 
            is = connection.getInputStream() ;
            StringBuffer sb = new StringBuffer() ; 
            int rl = -1 ; 
            byte[] buf = new byte[1024] ; 
            while( (rl=is.read(buf))!=-1 ){
                sb.append(new String(buf, 0, rl, charset)) ; 
            }
            ret = sb.toString() ; 
            connection.disconnect() ;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if( is != null ){
                try {
                    is.close() ;
                } catch (IOException e) {
                    e.printStackTrace();
                } 
            }
        }
        System.out.println(ret);
    }

至于如何在连接网络的时候弹出IE的代理框让用户输入不知道怎么实现的

还有获取IE代理也不知道怎么获取的

转载于:https://www.cnblogs.com/ooi-/p/4029100.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值