httpClient连接网站的问题 (URL带参数)

wzw12315 wzw12315 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
	private int getTotalPages() {

int num = 0;
// 通过httpClient获取网页响应,将返回的响应解析为纯文本
String url = "http://datainterface.eastmoney.com//EM_DataCenter/js.aspx?type=SR&sty=GGSR&js={%22data%22:[(x)],%22pages%22:%22(pc)%22,%22update%22:%22(ud)%22,%22count%22:%22(count)%22}&ps=50&p=2&mkt=0&stat=0&cmd=2&code=&rt=51302552";
HttpGet httpGet = new HttpGet(url);

httpGet.setConfig(RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build());
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;

String responseStr = "";
try {
httpClient = HttpClientBuilder.create().build();
HttpClientContext context = HttpClientContext.create();
response = httpClient.execute(httpGet, context);
int state = response.getStatusLine().getStatusCode();
if (state != 200)
responseStr = "";
HttpEntity entity = response.getEntity();
if (entity != null)
responseStr = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null)
response.close();
if (httpClient != null)
httpClient.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}

if (responseStr == null)
return 0;

JSONObject jsonbs = JSON.parseObject(responseStr);

num = Integer.parseInt(jsonbs.getString("pages"));

return num;
}



maven依赖包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.3</version>
</dependency>

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

代码如上,我直接使用代码中的URL(带了请求参数的)放到浏览器的网址框中运行,是可以正确打开网页的,但是使用httpclient方式又不行了,报 Source not found.的错误;如果代码的URL不带参数,倒是可以返回来200,表示正确响应了。
但是换了其他网址也是带请求参数的,httpclient又可以正确运行

这是什么原因呢?是东方财富网址使用了一些什么技术设置了吗?还是httpclient需要设置一些参数才可以正确使用??

...全文
评论 1
收藏 已收藏
分享
返回顶部
wzw12315
Java 51,407 成员 86,126 内容