简体   繁体   English

如何发送获取请求,就像从浏览器发送一样

[英]how to send get request as if sent from browser

I'm trying to test several APIs and compare them. 我正在尝试测试几个API并进行比较。 For one of the rest services I can only get response when I type the request in the browser. 对于其余的服务之一,我只能在浏览器中键入请求时得到响应。 When I send request from code (Java with Jersey) with the exact same URL I get 401 unauthorized. 当我使用完全相同的URL从代码(带有Jersey的Java)发送请求时,得到401未经授权。

As a workaround and also out of curiosity I want to have my java client send requests as if it was a browser so I can easily test the responses. 作为一种解决方法,也是出于好奇,我希望让Java客户端像浏览器一样发送请求,以便我可以轻松地测试响应。

this is purely for benchmarking... I'm aware that it's not a robust solution. 这纯粹是用于基准测试...我知道这不是一个可靠的解决方案。

my code for get request: 我的代码获取请求:

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class ClientTest
{

    @Test
    public void testGisGraphy()
    {

        String url = "http://services.gisgraphy.com/geocoding/geocode?address=89%20Rue%20Champoiseau%2C%20Tours&country=FR&format=JSON&postal=true";
        Client client = Client.create();
        WebResource webResource = client.resource(url);
        ClientResponse response =  webResource.accept("application/json").get(ClientResponse.class);
        String jsonString = response.getEntity(String.class);
        System.out.println(jsonString.toString());
    }
}

The following worked. 以下工作。
Two headers, "Accept-Encoding" with value "gzip" and "Accept-Language" with any arbit value are required. 需要两个标头,其值为“ gzip”的 “ Accept-Encoding”和具有任意仲裁值的“ Accept-Language” Accept Encoding with value gzip is must. 必须接受带有值gzip的编码。 Accept Language with any arbit value is fine. 接受任何仲裁值的语言都可以。

public static void main(String[] args) {

String url =
    "http://services.gisgraphy.com/geocoding/geocode?address=89%20Rue%20Champoiseau%2C%20Tours&country=FR&format=JSON&postal=true";
Client client = Client.create();
WebResource webResource = client.resource(url);
ClientResponse response =
    webResource
        .accept("application/json")
        .header("Accept-Encoding","gzip")
        .header("Accept-Language", "arbit text")
        .get(ClientResponse.class);
String jsonString = response.getEntity(String.class);
System.out.println(jsonString.toString());
}

I tried running it continuously. 我尝试连续运行它。 Following is what the response was: 以下是响应:

 <html> <head> <title>Too much requests</title> <style> body { font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body bgcolor="white" text="black"> <table width="100%" height="100%"> <tr> <td align="center" valigin="middle"> Your request can not be processed : <ul> <li>Are you a bot or do you use those webservices in a software ? you can only use the webservice in a browser</li> <li>Do you reach the maximum number of requests / seconds allowed</li> </ul> <br/><br/> Wait a little bit and resubmit your request </br></br/> If you want to have better QOS and SLA, you can <a href="http://premium.gisgraphy.com/">subscribe to premium webservices</a> </td> </tr> </table> </body> </html> 

Conclusion:- On the server side they are making some checks so that only requests from browsers are serviced and rest are unauthorized. 结论:-在服务器端,他们正在进行一些检查,以便仅处理来自浏览器的请求,其余未授权。

Doing a trial and error, it seems request headers "Accept-Encoding" and "Accept-Language" are must in the request. 反复尝试,看来请求标头“ Accept-Encoding”和“ Accept-Language”必须在请求中。

您应该指定用户代理标题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM