解决Server returned HTTP response code: 403 for URL报错

前言

在调用某个接口的时候,突然就遇到了Server returned HTTP response code: 403 for URL报错这个报错,导致获取不到接口的数据;
一开始,查到一个大部分说是

HttpURLConnection conn = (HttpURLConnection) url.openConnection()

这里加入

httpUrlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

但是发现并没有效果

后面,又查找到一个说是给它加一个

conn.setRequestProperty("User-Agent", "Mozilla/4.76");

然后结果成功解决了403的报错。

原因

对于原因并不是特别清楚,就我同事而言,说是因为我

在接口内部调用接口,套娃了,导致了这个问题;

查找的地方,说是:

不要在java中使用URLConnection,不接受使用 urlConnection 的普通 java 。
访问互联网.要访问浏览器,它需要执行搜索,没有例外会导致 
HTTP response code : 403 for URL
但是我本身是使用的HttpURLConnection,并且,如果你使用HttpURLConnection,
应该按照我后面的添加setRequestProperty

以下贴出我使用的apache依赖和post请求代码

依赖

本次接口调用为使用的apache

        <dependency>
            <groupId>org.apache.httpcomponentsgroupId>
            <artifactId>httpcoreartifactId>
            <version>4.4.10version>
        dependency>
        <dependency>
            <groupId>org.apache.httpcomponentsgroupId>
            <artifactId>httpclientartifactId>
            <version>4.5.5version>
        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 = "";

        //创建连接
        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的报错问题过程

你可能感兴趣的:(java,springboot,java,apache,http)