完美解決Server?returned?HTTP?response?code:403?for?URL報錯問題
前言
在調(diào)用某個接口的時候,突然就遇到了Server returned HTTP response code: 403 for URL報錯這個報錯,導(dǎo)致獲取不到接口的數(shù)據(jù);
一開始,查到一個大部分說是
HttpURLConnection conn = (HttpURLConnection) url.openConnection()
這里加入
httpUrlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
但是發(fā)現(xiàn)并沒有效果
后面,又查找到一個說是給它加一個
conn.setRequestProperty("User-Agent", "Mozilla/4.76");
然后結(jié)果成功解決了403的報錯。
原因
對于原因并不是特別清楚,就我同事而言,說是因為我
在接口內(nèi)部調(diào)用接口,套娃了,導(dǎo)致了這個問題;
查找的地方,說是:
不要在java中使用URLConnection,不接受使用 urlConnection 的普通 java 。 訪問互聯(lián)網(wǎng).要訪問瀏覽器,它需要執(zhí)行搜索,沒有例外會導(dǎo)致 HTTP response code : 403 for URL 但是我本身是使用的HttpURLConnection,并且,如果你使用HttpURLConnection, 應(yīng)該按照我后面的添加setRequestProperty
以下貼出我使用的apache依賴和post請求代碼
依賴
本次接口調(diào)用為使用的apache
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.10</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> </dependency>
post請求
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; public class PostClientUtil { public static String sendPost(String url,String param){ OutputStreamWriter out =null; BufferedReader reader = null; String response = ""; //創(chuàng)建連接 try { URL httpUrl = null; //HTTP URL類 用這個類來創(chuàng)建連接 //創(chuàng)建URL httpUrl = new URL(url); //建立連接 HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); // conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("User-Agent", "Mozilla/4.76"); conn.setUseCaches(false);//設(shè)置不要緩存 conn.setInstanceFollowRedirects(true); conn.setDoOutput(true); conn.setDoInput(true); conn.connect(); //POST請求 out = new OutputStreamWriter( conn.getOutputStream()); out.write(param); out.flush(); //讀取響應(yīng) reader = new BufferedReader(new InputStreamReader( conn.getInputStream())); String lines; while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); response+=lines; } reader.close(); // 斷開連接 conn.disconnect(); } catch (Exception e) { System.out.println("發(fā)送 POST 請求出現(xiàn)異常!"+e); e.printStackTrace(); } //使用finally塊來關(guān)閉輸出流、輸入流 finally{ try{ if(out!=null){ out.close(); } if(reader!=null){ reader.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return response; } }
結(jié)語
以上,就是本人解決請求接口403的報錯問題過程
到此這篇關(guān)于解決Server returned HTTP response code:403 for URL報錯問題的文章就介紹到這了,更多相關(guān)403 for URL報錯內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Javaweb監(jiān)聽器實例之統(tǒng)計在線人數(shù)
這篇文章主要為大家詳細介紹了Javaweb監(jiān)聽器實例之統(tǒng)計在線人數(shù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11解決IDEA克隆代碼后在右下角沒有g(shù)it分支的問題
這篇文章主要介紹了解決IDEA克隆代碼后在右下角沒有g(shù)it分支的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02java?stream?distinct()如何按一個或多個指定對象字段進行去重
這篇文章主要介紹了java?stream?distinct()如何按一個或多個指定對象字段進行去重問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12nodejs連接dubbo服務(wù)的java工程實現(xiàn)示例
這篇文章主要介紹了在項目遷移中,nodejs連接dubbo服務(wù)的java工程實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03