java - 用HttpClient中CloseableHttpClient请求https://www.zomato.com/ncr无法响应
阿神
阿神 2017-04-18 10:51:44
0
0
610

请求链接:https://www.zomato.com/ncr

方式一: 请求方式CloseableHttpClient

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(100);
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
HttpGet getMethod = new HttpGet("https://www.zomato.com/ncr");
getMethod.addHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
getMethod.addHeader("accept", "text/html");
getMethod.addHeader("accept-encoding", "gzip");
getMethod.addHeader("accept-language", "zh-CN");
CloseableHttpResponse execute = httpClient.execute(getMethod);
System.out.println(execute.getEntity().getContent());

问题请求无响应 查看源码得知发送请求头后未收到响应头

方式二:请求方式HttpClient

HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod("https://www.zomato.com/ncr");
getMethod.addRequestHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
getMethod.addRequestHeader("accept", "text/html");
getMethod.addRequestHeader("accept-encoding", "gzip");
getMethod.addRequestHeader("accept-language", "zh-CN");
int i = client.executeMethod(getMethod);
System.out.println(i);
byte[] responseBody = getMethod.getResponseBody();
System.out.println(responseBody.length);

能够正常响应

想问的问题

  • 为什么方式一请求无响应,如何解决

  • 方式一与方式二在发送请求时有什么区别

阿神
阿神

闭关修行中......

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板