可用的code

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public static String doPost(String url, String params, String contentType) /*throws IOException */{
CloseableHttpClient client = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(httpSoTimeout).setConnectTimeout(connectionTimeout).build();//设置请求和传输超时时间 String strResult = null;
try {
HttpPost post = new HttpPost(url);
post.setConfig(requestConfig);
if (StringUtils.isNotBlank(contentType)) {
post.setHeader("Content-Type", contentType);
}
StringEntity se = new StringEntity(params);
post.setEntity(se); HttpResponse response = client.execute(post); if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK && response.getEntity() != null){
HttpEntity entity = response.getEntity();
strResult = EntityUtils.toString(entity, "utf-8");
EntityUtils.consume(entity);
}
} catch (Exception e) {
log.error("http post error:{} ", e.getMessage());
return null;
}
return strResult;
}
}

最近用到了HttpClient写爬虫,可能我有新版本强迫症,老是喜欢用新版本的东西(虽说新版本不一定好用),然后就用了HttpClient 4.3。HttpClient这货和Lucene一样,每个版本的API都变化很大,这有点让人头疼。就好比创建一个HttpClient对象吧,每一个版本的都不一样,

3.X是这样的

HttpClient httpClient=new DefaultHttpClient();

4.3是这样的

CloseableHttpClient httpClient = HttpClients.createDefault();

当然,上面这些变化只不过是一些小变化,大家看看API大家就都会了。

我要讲的是超时设置,HttpClient有三种超时设置,最近比较忙,没时间具体归纳总结,以后再补上,我这里就讲一些最简单最易用的超时设置方法。

这是个3.X的超时设置方法

HttpClient client = new HttpClient();
client.setConnectionTimeout(30000);
client.setTimeout(30000);
HttpClient httpClient= new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

4.X版本的超时设置(4.3后已过时)

HttpClient httpClient=new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,2000);//连接时间
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,2000);//数据传输时间

4.3版本超时设置

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet=new HttpGet("http://www.baidu.com");//HTTP Get请求(POST雷同)
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();//设置请求和传输超时时间
httpGet.setConfig(requestConfig);
httpClient.execute(httpGet);//执行请求

BTW,4.3版本不设置超时的话,一旦服务器没有响应,等待时间N久(>24小时)。

org.apache.http.client.HttpClient; HttpClient 4.3超时设置的更多相关文章

  1. Java中httpClient中三种超时设置

    本文章给大家介绍一下关于Java中httpClient中的三种超时设置小结 在Apache的HttpClient包中,有三个设置超时的地方: /* 从连接池中取连接的超时时间*/ ConnManage ...

  2. apache.http.client.HttpClient

    前言 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java net包中已经提 ...

  3. org.apache.commons.httpclient和org.apache.http.client区别(转)

    官网说明: http://hc.apache.org/httpclient-3.x/ Commons HttpClient项目现已结束,不再开发.它已被其HttpClient和HttpCore模块中的 ...

  4. org.apache.http.client.HttpClient使用方法

    一.org.apache.commons.httpclient和org.apache.http.client区别(转)   官网说明: http://hc.apache.org/httpclient- ...

  5. Java之网络请求工具类(依赖:org.apache.http;注:HttpClient 4.4,HttpCore 4.4)

    到此处可以去下载依赖包:http://hc.apache.org/downloads.cgi import java.util.List; import org.apache.http.HttpSta ...

  6. org.apache.commons.httpclient.HttpClient的使用(转)

    HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java net包中已经提供了访 ...

  7. org.apache.http.client.CircularRedirectException: Circular redirect to "http://xxx"问题解决

      org.apache.http.client.CircularRedirectException: Circular redirect to "http://xxx"问题解决 ...

  8. httpclient发送request请求时设置header和timeout

    package com.xxx.xxx.common; import java.io.BufferedReader; import java.io.InputStreamReader; import ...

  9. Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页

    Apache HttpComponents Client 4.0已经发布多时,httpclient项目从commons子项目挪到了HttpComponents子项目下,httpclient3.1和 h ...

随机推荐

  1. 使用事件捕获实时捕获img是否加载完毕, 实现iframe内容高度自动适应

    如何判断在html中图片加载完毕呢? 给img图片加onload事件呗. 如何判断一个界面中所有的图片加载完毕呢? 给所有的图片加上onload事件呗. 如果有1000张图片那要怎么绑定事件呢? 我们 ...

  2. 区间DP HDU 2476

    两个字符串s1,s2 从s1->s2 最少刷几次 刷 i->j 都变成一样的+1 #include<stdio.h> #include<string.h> usin ...

  3. 16 IO操作文件读写

    IO的分类 第一种分法: 1.输入流 2.输出流 第二种分法: 1.字节流 2.字符流 第三种分法: 1.节点流 2.处理流 I/O当中的核心类: InputStream  <--------F ...

  4. ASP.NET MVC 扩展HtmlHelper类方法

    1.扩展HtmlHelper类方法ShowPageNavigate 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  5. js-JavaScript高级程序设计学习笔记5

    第七章 函数表达式 1.函数声明的一个重要特征就是函数声明提升,意思是在执行代码之前会先读取函数声明,因此可以把函数声明放在调用它的语句后面. 2.使用函数表达式创建的函数叫做匿名函数(拉姆达函数), ...

  6. 【BZOJ-1336&1337】Alie最小圆覆盖 最小圆覆盖(随机增量法)

    1336: [Balkan2002]Alien最小圆覆盖 Time Limit: 1 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 1573   ...

  7. 【uoj149】 NOIP2015—子串

    http://uoj.ac/problem/149 (题目链接) 题意 给出两个字符串A.B,问从A中取出k个互不重叠的子串按顺序组成B的方案数. Solution 一看这种题目就是字符串dp,字符串 ...

  8. bzoj2683简单题

    #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> ...

  9. C++开发的基于TCP协议的内网聊天工具

    项目相关地址 源码:https://github.com/easonjim/TCPChat bug提交:https://github.com/easonjim/TCPChat/issues

  10. waf2控件名

    1,查询表格(queryGrid),编辑表格(editGrid) wafGrid 2,快速F7 wafPromptQuick 3,表格F7 wafPromptGrid 4,自定义F7 wafPromp ...