gpt4 book ai didi

httpclient - 403 响应与 HttpClient 但不是与浏览器

转载 作者:行者123 更新时间:2023-12-03 16:41:14 28 4
gpt4 key购买 nike

我在使用 java 中的 HttpClient 库时遇到问题。

目标网站在 SSL ( https://www.betcris.com ) 上,我可以从该网站加载索引页面就好了。

但是,显示不同运动赔率的不同页面会使用 HttpClient 返回 403 响应代码,但在浏览器中加载相同页面效果很好。

这是这样一个页面:https://www.betcris.com/en/live-lines/soccer .

我开始使用 HttpFox(类似于 LiveHttpHeaders 的 Firefox 附加组件)收集的信息对此页面进行故障排除,确保我拥有所有正确的请求 header 和 cookie,但我无法使用 HttpClient 加载它。我还确定 cookie 与问题无关,因为我可以在浏览器中删除该网站的所有 cookie,然后直接点击该页面,它就会加载。

我通过使用在线工具 http://www.therightapi.com/test 确认这些页面有一些特别之处。 .此工具允许您输入页面的 url 以及您想要的任何请求 header ,并显示您从目标网站获得的响应。使用该工具,我可以加载 https://www.google.com很好,但我在尝试加载 https://www.betcris.com/en/live-lines/soccer 时遇到相同的 403 错误.

这是我在 therightapi 的设置:

enter image description here

和回应:

enter image description here

有谁知道这里发生了什么?

谢谢。

编辑:我已经创建了一个测试项目,这里是 java 代码,然后是你应该在 pom 中的 maven 依赖:

package com.yourpackage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

public class TestHttpClient {
public static void main(String[] args) {
String url = "https://www.betcris.com/en/live-lines/soccer";

HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);

// add request header
request.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0");
try {
HttpResponse response = client.execute(request);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());

BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}



<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>

最佳答案

我通过设置 解决了这个问题(避免了 403)用户代理 提出请求时的属性如下:

  • 如果您使用 HttpClient
    HttpGet httpGet = new HttpGet(URL_HERE);
    httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)
    AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
  • 如果您使用 HttpURLConnection
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();    
    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)
    AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
  • 关于httpclient - 403 响应与 HttpClient 但不是与浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46604840/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com