首页 > 其他分享 >完美解决Server returned HTTP response code:403 for URL报错问题

完美解决Server returned HTTP response code:403 for URL报错问题

时间:2023-09-13 12:24:51浏览次数:36  
标签:code HTTP URL 403 报错 apache import conn

在调用某个接口的时候,突然就遇到了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报错内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:

标签:code,HTTP,URL,403,报错,apache,import,conn
From: https://www.cnblogs.com/roak/p/17699254.html

相关文章