iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >完美解决Server returned HTTP response code:403 for URL报错问题
  • 455
分享到

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

请求接口403的报错403 for URL报错 2023-03-09 18:03:25 455人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录前言原因依赖post请求结语前言 在调用某个接口的时候,突然就遇到了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.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 = "";

        //创建连接
        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报错内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

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

本文链接: https://www.lsjlt.com/news/199141.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作