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

posted @ 2014-10-16 16:19  行-云  阅读(518)  评论(0编辑  收藏  举报