您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“apache Server returned HTTP response code:403 for URL報錯問題如何解決”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“apache Server returned HTTP response code:403 for URL報錯問題如何解決”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
在調(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的報錯。
對于原因并不是特別清楚,就我同事而言,說是因?yàn)槲?/p>
在接口內(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>
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; } }
讀到這里,這篇“apache Server returned HTTP response code:403 for URL報錯問題如何解決”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動手實(shí)踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。