搜尋

首頁  >  問答  >  主體

java - 用HttpClient中CloseableHttpClient请求https://www.zomato.com/ncr无法响应

请求链接: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);

能够正常响应

想问的问题

阿神阿神2803 天前607

全部回覆(0)我來回復

無回覆
  • 取消回覆