欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

完美解決Server?returned?HTTP?response?code:403?for?URL報錯問題

 更新時間:2023年03月09日 11:10:43   作者:相與還  
在調(diào)用某個接口的時候,突然就遇到了Server?returned?HTTP?response?code:?403?for?URL報錯這個報錯,導(dǎo)致獲取不到接口的數(shù)據(jù),下面小編給大家分享解決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)文章

最新評論