前言
在調(diào)用某個(gè)接口的時(shí)候,突然就遇到了Server returned HTTP response code: 403 for URL報(bào)錯(cuò)這個(gè)報(bào)錯(cuò),導(dǎo)致獲取不到接口的數(shù)據(jù);
一開始,查到一個(gè)大部分說是
HttpURLConnection conn = (HttpURLConnection) url.openConnection()
這里加入
httpUrlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
但是發(fā)現(xiàn)并沒有效果
后面,又查找到一個(gè)說是給它加一個(gè)
conn.setRequestProperty("User-Agent", "Mozilla/4.76");
然后結(jié)果成功解決了403的報(bào)錯(cuò)。
原因
對(duì)于原因并不是特別清楚,就我同事而言,說是因?yàn)槲?/p>
在接口內(nèi)部調(diào)用接口,套娃了,導(dǎo)致了這個(gè)問題;
查找的地方,說是:
不要在java中使用URLConnection,不接受使用 urlConnection 的普通 java 。
訪問互聯(lián)網(wǎng).要訪問瀏覽器,它需要執(zhí)行搜索,沒有例外會(huì)導(dǎo)致
HTTP response code : 403 for URL
但是我本身是使用的HttpURLConnection,并且,如果你使用HttpURLConnection,
應(yīng)該按照我后面的添加setRequestProperty
以下貼出我使用的apache依賴和post請(qǐng)求代碼
依賴
本次接口調(diào)用為使用的apache文章來源:http://www.zghlxwxcb.cn/news/detail-602800.html
<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請(qǐng)求
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類 用這個(gè)類來創(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請(qǐng)求
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 請(qǐng)求出現(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é)語
以上,就是本人解決請(qǐng)求接口403的報(bào)錯(cuò)問題過程文章來源地址http://www.zghlxwxcb.cn/news/detail-602800.html
到了這里,關(guān)于解決Server returned HTTP response code: 403 for URL報(bào)錯(cuò)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!