curl请求成功httpclient却一直是超时

    项目以前运行好好的,突然请求一直访问超时,使用cookies登录却发现自己电脑上可以登录,但是线上的任务一直失败,由此怀疑可能cookies和主机绑定了,不能异地登录。使用curl请求线上和本地的cookie都可以返回response,难道curl和httpclient或者UrlConnection还不一样吗?
    经过测试还真的不一样,我的curl请求,删除多余的header和cookie如下:

curl -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36' --data-binary '{"username":"admin","password":"123456"}' --compressed 'https://www.blibli.com/backend/common/users/_login'

但是代码中却要把所有的header写上,估计是网站反爬做了限制,经过测试了多次才跑通:

AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
builder.setConnectTimeout(3000);    // 连接超时
builder.setReadTimeout(2000);        // 读取数据超时
builder.setMaxConnections(1000);    // 最大连接数
builder.setSSLContext(SSLContext.getDefault());
AsyncHttpClient asyncHttpClient = new AsyncHttpClient(builder.build());
FluentCaseInsensitiveStringsMap headers= new FluentCaseInsensitiveStringsMap();
headers.add("authority","www.blibli.com");
headers.add("Host","www.blibli.com");
headers.add("origin","https://www.blibli.com");
headers.add("content-type","application/json;charset=UTF-8");
headers.add("user-agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36");
Request request = new RequestBuilder()
    .setHeaders(headers)
    .setUrl("https://www.blibli.com//backend/common/users/_login")
    .setBody("{\"username\":\"admin\",\"password\":\"123456\"}")
    .setMethod("post")
    .build();
ListenableFuture response = asyncHttpClient.executeRequest(request);
System.out.println(response.get().getResponseBody());

https协议的请求,还要信任所有证书:
https://www.jianshu.com/p/4dc94077c9dc

你可能感兴趣的:(curl请求成功httpclient却一直是超时)