http使用代理

来源:互联网 发布:lamp环境搭建 源码包 编辑:程序博客网 时间:2024/04/29 08:34
if (null == strUrl) {                    System.out.println("URL信息不存在!");                    return null;                } else {                    System.out.println("URL信息:" + strUrl);                }                // 创建HttpURLConnection对象                java.net.URL url = new java.net.URL(strUrl);                java.net.URLConnection urlcn = null;                if (isUserProxy) {                    SocketAddress add = new InetSocketAddress(host, intPort);                    Proxy p = new Proxy(Proxy.Type.HTTP, add);                    urlcn = url.openConnection(p);                    if ((null != proxyUser) && !"".equals(proxyUser)) {                        // 用户名密码需要以BASE64编码设置,无用户名密码的代理环境不需要设置。                        urlcn.setRequestProperty("Proxy-Authorization",                                                 "Basic "                                                 + new sun.misc.BASE64Encoder().encode((proxyUser + ":" + proxyPassword)                                                                                       .getBytes()));                    }                } else {                    urlcn = url.openConnection();                }                if (urlcn == null) {                    System.out.println("URL连接失败【" + strUrl + "】");                    return null;                }                if (urlcn instanceof HttpURLConnection) {                    httpURLConnection = (HttpURLConnection) urlcn;                    httpURLConnection.setRequestMethod("POST");                    if (httpURLConnection != null) {                        httpURLConnection.setDoOutput(true);                        httpURLConnection.setDoInput(true);                        // 发送请求                        java.io.OutputStream out = null;                        out = httpURLConnection.getOutputStream();                        out.write(strBody.getBytes());                        out.flush();                        out.close();                        String line = null;                        // 添加完毕                        BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection                                                                                                                                                               .getInputStream()));                        line = reader.readLine();                        if (null != line) {                            str.append(line);                        }                        while ((line = reader.readLine()) != null) {                            str.append(System.getProperty("line.separator")).append(line);                        }                        reader.close();                        httpURLConnection.disconnect();


原创粉丝点击