在調用某個接口的時候,突然就遇到了Server returned HTTP response code: 403 for URL報錯這個報錯,導致獲取不到接口的數據,下面小編給大家分享解決Server returned HTTP response code:403 for URL報錯問題,感興趣的朋友一起看看吧
前言
在調用某個接口的時候,突然就遇到了Server returned HTTP response code: 403 for URL報錯這個報錯,導致獲取不到接口的數據;
一開始,查到一個大部分說是
1
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection() |
這裏加入
1
|
httpUrlConn.setRequestProperty( "User-Agent" , "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)" ); |
但是發現並沒有效果
後面,又查找到一個說是給它加一個
1
|
conn.setRequestProperty( "User-Agent" , "Mozilla/4.76" ); |
然後結果成功解決了403的報錯。
原因
對於原因並不是特別清楚,就我同事而言,說是因爲我
在接口內部調用接口,套娃了,導致了這個問題;
查找的地方,說是:
1
2
3
4
5
|
不要在java中使用URLConnection,不接受使用 urlConnection 的普通 java 。 訪問互聯網.要訪問瀏覽器,它需要執行搜索,沒有例外會導致 HTTP response code : 403 for URL 但是我本身是使用的HttpURLConnection,並且,如果你使用HttpURLConnection, 應該按照我後面的添加setRequestProperty |
以下貼出我使用的apache依賴和post請求代碼
依賴
本次接口調用爲使用的apache
1
2
3
4
5
6
7
8
9
10
|
<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請求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
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 = "" ; //創建連接 try { URL httpUrl = null ; //HTTP URL類 用這個類來創建連接 //創建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 ); //設置不要緩存 conn.setInstanceFollowRedirects( true ); conn.setDoOutput( true ); conn.setDoInput( true ); conn.connect(); //POST請求 out = new OutputStreamWriter( conn.getOutputStream()); out.write(param); out.flush(); //讀取響應 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( "發送 POST 請求出現異常!" +e); e.printStackTrace(); } //使用finally塊來關閉輸出流、輸入流 finally { try { if (out!= null ){ out.close(); } if (reader!= null ){ reader.close(); } } catch (IOException ex){ ex.printStackTrace(); } } return response; } } |
結語
以上,就是本人解決請求接口403的報錯問題過程
到此這篇關於解決Server returned HTTP response code:403 for URL報錯問題的文章就介紹到這了,更多相關403 for URL報錯內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支持腳本之家!