403使用HttpClient响应,但不使用浏览器

时间:2017-10-06 11:36:15

标签: httpclient

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

目标网站使用SSL(https://www.betcris.com),我可以从该网站加载索引页面。

但是,显示不同体育赛事赔率的不同页面会返回带有HttpClient的403响应代码,但在浏览器中加载相同的页面效果很好。

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

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

我确认使用http://www.therightapi.com/test上的在线工具,这些页面会有一些特别之处。此工具允许您输入页面的URL以及所需的任何Request标头,并显示您从目标网站获得的响应。使用该工具,我可以正常加载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>

4 个答案:

答案 0 :(得分:1)

我通过设置 User-Agent 属性来解决了这个问题(避免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");
    

答案 1 :(得分:0)

403 Forbidden用于表示身份验证要求。事实上,完整的403响应应该告诉你这一点。幸运的是,HttpClient can do authentication

答案 2 :(得分:0)

我使用以下代码来使用HTTPS Url:

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;

...

    SSLContext sslContext =
            new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build();

    try (CloseableHttpClient httpClient = HttpClients.custom().setSSLContext(sslContext)
                                                     .setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
        HttpGet httpGet = new HttpGet("YOUR_HTTPS_URL");
        httpGet.setHeader("Accept", "application/xml");
        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");

        HttpResponse response = httpClient.execute(httpGet);

        logger.info("Response: " + response);

    }

的pom.xml:

 <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
 </dependency>

答案 3 :(得分:0)

就我而言,Web服务器不使用代理与API通信。

我只是在 web.config 中的 system.net 下取消了 defaultproxy

  <system.net>
    <defaultProxy enabled="false" />
   </system.net>