码迷,mamicode.com
首页 > 其他好文 > 详细

通过代理访问网络

时间:2014-10-16 18:37:12      阅读:456      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   for   

使用代理方式连接到网络

@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代理也不知道怎么获取的

通过代理访问网络

标签:style   blog   http   color   io   os   使用   ar   for   

原文地址:http://www.cnblogs.com/ooi-/p/4029100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!