httpClient连接网站的问题 (URL带参数)
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需要设置一些参数才可以正确使用??
...全文
相关推荐
java使用HttpClient通过url下载文件到本地
在这个特定的场景中,我们利用HttpClient来从指定的URL下载文件到本地。以下是对这个主题的详细阐述:1.
httpUtil httpclient 登陆携带cookie访问下一个连接
**配置HttpClient**:为了管理HTTP连接,我们需要创建一个`HttpClient`实例。可以使用`HttpClientBuilder`来定制各种设置,如超时时间、重试策略、连接池等。
使用java的HttpClient实现多线程并发
通过使用HttpClient的连接池和多线程,我们可以有效地并发执行HTTP GET请求,同时控制并发数量,避免过多的网络连接导致服务器压力过大。
JAVA中三种URL连接方法
它封装了URL的所有组成部分,如协议、主机名、端口号和路径。通过`URL`类,我们可以创建一个URL对象,然后使用`openConnection()`方法打开与该URL的连接。
httpclient自动注册网站
**构建请求URL**:根据目标网站的注册接口,确定HTTP请求的URL。这可能是一个特定的注册页面或API端点。3.


