今天通過nginx代理下載pdf報這個錯,網(wǎng)上查了很多資料,說的大部分都是Android studio這個的解決辦法,幾乎沒有針對nginx的處理,部分說是需要配hosts,配了hosts也沒用,有一些說是要增加proxy_set_header Host $http_host;這個配置,也不能解決我的問題,這邊記錄一下針對我這邊出現(xiàn)問題處理方式。
代碼相對簡單
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//設(shè)置超時間為5秒
conn.setConnectTimeout(5*1000);
//防止屏蔽程序抓取而返回403錯誤
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到輸入流
InputStream inputStream = conn.getInputStream();
計劃是這么直接訪問,但是想到?jīng)]辦法出網(wǎng),直接就報域名訪問不到,想到需要走代理,首先在nginx配置相關(guān)代理信息
server {
listen 8088;
#listen somename:8080;
server_name file.test.ess.tencent.cn;
location /file {
proxy_pass https://xxx.xx.xx/file;
proxy_cache_valid 200 1y;
add_header Nginx-Cache "$upstream_cache_status";
proxy_ssl_session_reuse off;
proxy_ssl_server_name on;
proxy_ssl_name xxx.xx.xx;
proxy_ssl_protocols TLSv1.2;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header Host $http_host;
}
}
配置完成后,想著是不是代碼要走代理修改代碼如下
URL url = new URL(urlStr);
Proxy proxy=new Proxy(Proxy.Type.HTTP, new InetSocketAddress(IP, PORT));
HttpURLConnection conn = (HttpURLConnection)url.openConnection(proxy);
//設(shè)置超時間為5秒
conn.setConnectTimeout(5*1000);
//防止屏蔽程序抓取而返回403錯誤
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到輸入流
InputStream inputStream = conn.getInputStream();
確認代理后就發(fā)現(xiàn)報題目中的錯誤,后來發(fā)現(xiàn)自己想復(fù)雜了,nginx已經(jīng)幫忙代理了,不需要這邊再次代理,直接把地址地換成自己的ip+端口即可,讓nginx去處理出網(wǎng)的問題
最終確認代碼和nginx配置如下:文章來源:http://www.zghlxwxcb.cn/news/detail-522646.html
urlStr = urlStr.replace("https://xxx.xx.xx","http://IP:PORT");
URL url = new URL(urlStr);
// Proxy proxy=new Proxy(Proxy.Type.HTTP, new InetSocketAddress(IP, PORT));
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//設(shè)置超時間為5秒
conn.setConnectTimeout(5*1000);
//防止屏蔽程序抓取而返回403錯誤
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到輸入流
InputStream inputStream = conn.getInputStream();
server {
listen 8088;
#listen somename:8080;
server_name xxx.xx.xx;
location / {
proxy_pass https://xxx.xx.xx;
proxy_cache_valid 200 1y;
add_header Nginx-Cache "$upstream_cache_status";
proxy_ssl_session_reuse off;
proxy_ssl_server_name on;
proxy_ssl_name xxx.xx.xx;
proxy_ssl_protocols TLSv1.2;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header Host $http_host;
}
}
記錄本次問題,轉(zhuǎn)載注明出處文章來源地址http://www.zghlxwxcb.cn/news/detail-522646.html
到了這里,關(guān)于Unable to tunnel through proxy. Proxy returns “HTTP/1.1 400 Bad request的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!