使用代理IP发送请求,出现502错误 java.io.IOException: Unable to tunnel through proxy. Proxy returns “HTTP/1.1 502
今天有客户咨询使用代理IP请求一个国外网址(https://read.qxmd.com/),这个网址在国内打开速度比较慢,然后报错了 java.io.IOException: Unable to tunnel through proxy. Proxy returns “HTTP/1.1 502 Bad Gateway”
HTTP 502 – 网关错误
此处一般指Nginx做反向代理服务器时,所连接的Web应用服务器无响应导致的。一般是因为后端的Tomcat,Jetty,Tornado等服务器没有启动。我们知道,Nginx属于HTTP服务器,不属于Web应用服务器。
出现“HTTP 502 – 网关错误”,需要重启Web应用服务器。
排查问题解决方案:
确保目标网站的URL能够正常打开,如果打不开或者长时间不响应(打开很慢),那么这种情况下考虑是网址URL有问题,没有更好的解决方案,只能忽略个别错误。
目标URL打开正常,所以试一下不使用代理IP的情况下,能否正常返回,如果返回正常,可以继续第三步排查。如果返回仍然为502或者50X的错误,那么这种情况下考虑是网址URL有问题,没有更好的解决方案,只能忽略个别错误。
考虑是代理IP在可用时间范围内,无法正确打开URL导致最终无响应,解决办法就是换下一个新IP。
下面是我的测试代码:
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.ProxyConfig;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;public class TestDynamicIp {public static List<String> ipList = new ArrayList<>();public static boolean gameOver = false;public static void main(String[] args) {long fetchIpSeconds = 3;int threadNum = 2;int testTime = 5;// 请填写无忧代理IP订单号,填写之后才可以提取到IP哦, https://www.data5u.com 获取String order = "请填写无忧代理IP订单号";// 这里换成百度贴吧的URL,可以把这里封装成一个方法,接收URL参数,实现不同的URL采集String targetUrl = "https://www.iqiyi.com/v_19rrlcvy50.html?pltfm=11&pos=title&flashvars=videoIsFromQidan%3Ditemviewclk_a#vfrm=5-6-0-1";// 是否加载JS,加载JS会导致速度变慢boolean useJS = false;// 请求超时时间,单位毫秒,默认5秒int timeOut = 5000;if (order == null || "".equals(order)) {System.err.println("请输入无忧代理IP动态代理订单号");return;}System.out.println(">>>>>>>>>>>>>>无忧代理动态IP测试开始<<<<<<<<<<<<<<");System.out.println("***************");System.out.println("接口返回IP为国内各地区,每次最多返回10个");System.out.println("提取IP间隔 " + fetchIpSeconds + " 秒 ");System.out.println("开启爬虫线程 " + threadNum);System.out.println("爬虫目标网址 " + targetUrl);System.out.println("测试次数 3 ");System.out.println("***************\n");TestDynamicIp tester = new TestDynamicIp();new Thread(tester.new GetIP(fetchIpSeconds * 1000, testTime, order)).start();for (int i = 0; i < threadNum; i++) {tester.new Crawler(200, targetUrl, useJS, timeOut).start();}while(!gameOver){try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(">>>>>>>>>>>>>>无忧代理动态IP测试结束<<<<<<<<<<<<<<");System.exit(0);}// 采集部分public class Crawler extends Thread{@Overridepublic void run() {while(!gameOver){webParseHtml(targetUrl);try {Thread.sleep(sleepMs);} catch (InterruptedException e) {e.printStackTrace();}}}long sleepMs = 200;boolean useJs = false;String targetUrl = "";int timeOut = 5000;public Crawler(long sleepMs, String targetUrl, boolean useJs, int timeOut) {this.sleepMs = sleepMs;this.targetUrl = targetUrl;this.useJs = useJs;this.timeOut = timeOut;}public String webParseHtml(String url) {String html = "";BrowserVersion[] versions = {BrowserVersion.CHROME, BrowserVersion.BEST_SUPPORTED};WebClient client = new WebClient(versions[(int)(versions.length * Math.random())]);try {client.addRequestHeader("Referer", "https://tieba.baidu.com/");client.getOptions().setThrowExceptionOnFailingStatusCode(false);client.getOptions().setJavaScriptEnabled(useJs);client.getOptions().setCssEnabled(false);client.getOptions().setThrowExceptionOnScriptError(false);client.getOptions().setTimeout(timeOut);client.getOptions().setAppletEnabled(true);client.getOptions().setGeolocationEnabled(true);client.getOptions().setRedirectEnabled(true);client.getOptions().setUseInsecureSSL(true);String ipport = getAProxy();if (ipport != null) {ProxyConfig proxyConfig = new ProxyConfig(ipport.split(":")[0], Integer.parseInt(ipport.split(":")[1]));client.getOptions().setProxyConfig(proxyConfig);}else {System.out.print(".");return "";}HtmlPage page = client.getPage(url);html = page.asXml();Document doc = Jsoup.parse(html);System.out.println(getName() + " 使用代理 " + ipport + "请求目标网址返回:" + doc.select("title").text() + doc.select("body").text());} catch (Exception e) {System.err.println(e.getMessage());} finally {client.close();}return html;}private String getAProxy() {if (ipList.size() > 0) {String ip = ipList.get((int)(Math.random() * ipList.size()));return ip ;}return null;}}// 定时获取动态IPpublic class GetIP implements Runnable{long sleepMs = 1000;int maxTime = 3;String order = "";public GetIP(long sleepMs, int maxTime, String order) {this.sleepMs = sleepMs;this.maxTime = maxTime;this.order = order;}@Overridepublic void run() {long getIpTime = 0;int time = 1;while(!gameOver){if(time >= 4){gameOver = true;break;}try {// http://soft.data5u.com/vpn/get/a/2d1bb3388b9182cfabc9f88cac1ffd68.html?ttl&areaCN=海南海口联通java.net.URL url = new java.net.URL("http://api.ip.data5u.com/dynamic/get.html?order=" + order + "&ttl&random=1");HttpURLConnection connection = (HttpURLConnection)url.openConnection();connection.setConnectTimeout(3000);connection = (HttpURLConnection)url.openConnection();InputStream raw = connection.getInputStream(); InputStream in = new BufferedInputStream(raw); byte[] data = new byte[in.available()];int bytesRead = 0; int offset = 0; while(offset < data.length) { bytesRead = in.read(data, offset, data.length - offset); if(bytesRead == -1) { break; } offset += bytesRead; } in.close(); raw.close();String[] res = new String(data, "UTF-8").split("\n");List<String> ipList = new ArrayList<>();for (String ip : res) {try {System.out.println(ip);String[] parts = ip.split(",");if (Integer.parseInt(parts[1]) > 0) {ipList.add(parts[0]);}} catch (Exception e) {}}if (ipList.size() > 0) {TestDynamicIp.ipList = ipList;System.out.println("第" + ++getIpTime + "次获取动态IP " + ipList.size() + " 个");time += 1;}} catch (Exception e) {e.printStackTrace();System.err.println(">>>>>>>>>>>>>>获取IP出错");}try {Thread.sleep(sleepMs);} catch (InterruptedException e) {e.printStackTrace();}}}}}
无忧代理IP(www.data5u.com)原创文章,转载请注明出处:https://www.data5u.com/help/article-139.html
使用代理IP发送请求,出现502错误 java.io.IOException: Unable to tunnel through proxy. Proxy returns “HTTP/1.1 502相关推荐
- 发送 POST 请求出现异常!java.io.IOException: Server returned HTTP response code: 400 for URL:
http发送post请求报400,405,记录下原因: 报错如下: 发送 POST 请求出现异常!java.io.IOException: Server returned HTTP response ...
- POST 请求出现异常!java.io.IOException: Server returned HTTP response code: 400 for URL
http发送post请求报400,405 报错如下: 发送 POST 请求出现异常!java.io.IOException: Server returned HTTP response code: 4 ...
- java: 在相应的 try 语句主体中不能抛出异常错误java.io.IOException
java: 在相应的 try 语句主体中不能抛出异常错误java.io.IOException 参考文章: (1)java: 在相应的 try 语句主体中不能抛出异常错误java.io.IOExcep ...
- keytool 错误 java.io.IOException: incorrect AVA format
用keytool生成sha1的,一定要有一个keystore 当然系统默认有一个c:\users\administrator\debug.keystore,密码android这样可以生成一个debug ...
- keytool 错误: java.io.IOException: Keystore was tampered with, or password was incorrect → 解决办法
keytool 错误: java.io.IOException: Keystore was tampered with, or password was incorrect → 解决办法 问题复现 我 ...
- keytool 错误: java.io.IOException: Keystore was tampered with, or password was incorrect----gt;解决方法
老前辈们说的对,干IT需要最多的是细心.老哥我打包打了一个多小时,也是奇迹了. 问题:在创建新签名的时候报错. keytool 错误: java.io.IOException: Keystore wa ...
- keytool 错误:java.io.IOException:Incorrect AVA format
在使用Android Studio生成签名文件的时候提示:keytool 错误:java.io.IOException:Incorrect AVA format 的错误,这个主要是我在填写证书里Org ...
- 《err keytool 错误: java.io.IOException: Keystore was tampered with, or password was incorrect》
错误日志: keytool 错误: java.io.IOException: Keystore was tampered with, or password was incorrect java.io ...
- mybatis错误——java.io.IOException Could not find resource comxxxxxxMapper.xml
在学习Mybatis的时候,参考网上的教程进行简单demo的搭建,配置的没有问题,然后出现了下面的错误! Exception in thread "main" java.lang. ...
最新文章
- 配置ssl证书_Nginx监听443端口配置SSL证书
- LibSVM学习(三)——LibSVM使用规范
- 【thinkPHP框架】Failed opening required 'header.php' include_path='.;c:\php5\pear 终级解决方案...
- 互联网(IT)大厂面试技巧(面经)
- linux远程计算,如何使用Linux应用服务器场的远程计算?
- 菲波那契数列(信息学奥赛一本通-T1188)
- c语言简单的动画程序代码,发个C代码(简单动画演示)
- Linux压缩文件与解压文件(*.zip)
- 三角网格表面高斯曲率的计算与可视化
- 情绪管理--不要总做“好脾气”的人。
- 软件体系结构——设计模式
- 我的注电之旅——基础考试篇
- axure如何竖立文字_Axure pr 如何让文字显示为竖着的?
- ESP32 开发笔记(四)LVGL控件学习 Canvas 画布控件
- java swing mysql实现的仓库商品管理系统
- 富文本编辑器 Kindeditor 的使用和 常见错误
- 自举电路工作原理和自举电阻和电容的选取
- 未发表的2015年年终总结
- 什么是5g卡,5g有啥好的
- 如何将图片放大不失真,保持清晰度?
热门文章
- 木马是如何入侵手机的
- 控制台应用于指挥中心的技术要求是什么?
- xquery_使用XQuery处理XML
- 【论文】强化学习必读经典论文 | 如何学习强化学习 | 强化学习入门
- 1433端口无法连接(sql server 数据库无法访问问题)解决思路
- 解决Shell Infrastructure Host / sihost.exe 在后台长期高额占用cpu
- Codeforces:896C-Willem, Chtholly and Seniorious(odt模板)
- 世界驰名咖啡(图片以及制作方法)
- Linux环境下GRE的部署
- 实战:如何降低云开发服务器成本?