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

这篇具有很好参考价值的文章主要介绍了解决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的报错问题过程文章来源地址https://www.toymoban.com/news/detail-602800.html

到了这里,关于解决Server returned HTTP response code: 403 for URL报错的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 问题解决——IDEA git 操作报错:The requested URL returned error: 403

    使用的命令有:git pull、git push。 报错内容: remote: [session-424579a9] Access denied fatal: unable to access \\\'https://gitee.com/xxxxxx.git/\\\': The requested URL returned error: 403 查看 git 的用户名、邮箱和密码,命令如下; 如果不对就修改成你期望的 git 用户名、邮箱和密码; 问题依然没有解决的话,那

    2024年02月06日
    浏览(40)
  • git 报错The requested URL returned error: 403

    目录 修改git 配置信息用户名和邮箱(无效) 修改当前项目下的git文件夹中的config(无效) 清除缓存(有效) 优化:每次拉取代码都要输入用户名和密码 今日更换电脑,从云效平台拉取代码报错。 原因:自己的账号和电脑上之前的Git账户有冲突,需清除缓存 主要参考:解

    2024年02月06日
    浏览(41)
  • 成功解决wget下载报错 : wget HTTP request sent, awaiting response... 403 Forbidden

    –2023-07-15 02:32:57-- https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2023.03-Linux-x86_64.sh Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)… 2402:f000:1:400::2, 101.6.15.130 Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|2402:f000:1:400::2|:443… connected. HTTP request sent, awaiting r

    2024年02月16日
    浏览(33)
  • NacosException: http error, code=403、NacosimeException——报错解决方法【Nacos2.x】

    连不上是因为成功开启 鉴权 后,所使用的Spring Cloud服务被拦截,需要在配置中添加Nacos用户名和密码: 首先确保MySQL服务已启动,随后,此处内容需要 注意修改Nacos的配置文件中的MySQL用户名和密码 , 打开Nacos文件夹 - conf -application.properties,将此处的db.user 和 db.password修改为

    2024年04月27日
    浏览(34)
  • git push 报错unable to access https://gitee.com/****/***.git/ : The requested URL returned error:403

    对于git小白来说,第一次使用git总会报各种稀奇古怪的错误。 最近的操作git的时候,就报了如上的错误fatal: unable to access  https://gitee.com/****/***.git/ : The requested URL returned error:403 对于这样的错误无非就是无权访问的问题, 解决的方法我试过的只有两种 : 1 .自己的git账户本身

    2023年04月08日
    浏览(35)
  • git push报错:fatal: unable to access ‘https://github.com/***/‘:The requested URL returned error: 403

    又是被自己菜死的一天。 使用git push命令后,报错信息如下: remote: Permission to *** denied to . fatal: unable to access \\\'https://github.com/ /\\\': The requested URL returned error: 403 当然在git push 之前生成teken是必要的,在设置里面开发者设置中Personal access tokens生成就可以了,注意生成之后需要复制

    2024年02月16日
    浏览(39)
  • 解决使用sourcetree推送(git push)代码提示 The requested URL returned error: 403 问题

    我使用sourcetree push 代码,他会显示403,也就是我当前的 push 是失败的,但是我直接在终端进行 git push origin HEAD:dev 它又是成功的! 终端能成功可能是因为直接我配置过 token ,它和 sourcetree 的区别可能就是出现在账号问题上(猜测 所以我的解决办法是将当前本地仓库的账号换

    2024年02月12日
    浏览(41)
  • 【异常解决】(二)解决docker报错Error response from daemon: Get... http: server gave HTTP response to HTTPS

    场景:本机个人电脑Windows系统安装了docker客户端,远程Linux服务器部署了镜像仓库,远程仓库可以接收别的服务器的镜像推送,但接收本机电脑镜像时失败(实际为推送失败)。使用docker login XXXX:XX:XX:XX:8081命令登录时,报错 Error response from daemon: Get “https://XXXX:XX:XX:XX:8081/v2

    2024年02月11日
    浏览(54)
  • 报错解决: CondaHTTPError: HTTP 000 CONNECTION FAILED for url ***

    问题描述 在我设置好国内源之后,用conda创建虚拟环境,下载python版本时出现以下错误。 Solving environment: failed CondaHTTPError: HTTP 000 CONNECTION FAILED for url https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch/repodata.json Elapsed: - An HTTP error occurred when trying to retrieve this URL. HTTP errors are of

    2024年02月16日
    浏览(35)
  • 拉取docker私有仓库镜像报错http: server gave HTTP response to HTTPs client解决办法

    sudo docker pull 10.246.152.91:5000/xxx_image Error response from daemon Get \\\"https://10.246.152.91:5000/v2/\\\": http: server gave HTTP response to HTTPs client 创建文件/etc/docker/daemon.json, 文件内容如下: { “insecure-registries”: [ “10.246.152.91:5000” ] } 重启Docker服务:sudo service docker restart

    2024年01月23日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包