专自:http://www.2cto.com/kf/201407/314279.html
package httpClientTest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javax.net.ssl.SSLException;
import org.apache.http.Consts;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.ConnectionRequest;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
public class Main {
public static void main(String[] args) {
try {
test9();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 测试1: 构建复杂uri,这种方式会很方便的设置多个参数;
*
* HttpClients类是client的具体一个实现类;
*
* URIBuilder包含:协议,主机名,端口(可选),资源路径,和多个参数(可选)
*
*/
private static void test1() {
CloseableHttpClient client = HttpClients.createDefault();
URI uri = null;
try {
uri = new URIBuilder()
.setScheme("http")
.setHost("webservice.webxml.com.cn")
.setPath("/WebServices/MobileCodeWS.asmx/getDatabaseInfo")
.setParameter("", "")//这里可以设置多个参数
.setParameter("", "")
.setParameter("", "")
.setParameter("", "")
.build();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
//http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getDatabaseInfo
HttpGet get = new HttpGet(uri);
try {
CloseableHttpResponse response = client.execute(get);
if(response.getStatusLine().getStatusCode()==200){
System.out.println(EntityUtils.toString(response.getEntity()));
//以下这种方式读取流也可以,只不过用EntityUtils会更方便
/*InputStream is = response.getEntity().getContent();
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len=-1;
while((len = is.read(buffer))!=-1){
os.write(buffer,0,len);
}
os.close();
is.close();
System.out.println(os.size()+new String(os.toByteArray(),"utf-8"));*/
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 为uri进行加密,并进行表单提交;
*
* 许多应用需要模拟提交的HTML表单的处理,例如,在
为了登录到Web应用程序或提交的输入数据。 HttpClient提供的实体类
UrlEncodedFormEntity可以实现该过程;
*/
private static void test2(){
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
//构建请求参数
List
list.add(new BasicNameValuePair("mobileCode", "110"));
list.add(new BasicNameValuePair("userID", ""));
//构建url加密实体,并以utf-8方式进行加密;
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, Consts.UTF_8);
httppost.setEntity(entity);
try {
CloseableHttpResponse response = client.execute(httppost);
if(response.getStatusLine().getStatusCode()==200){
//org.apache.http.util.EntityUtils类可以快速处理服务器返回实体对象
System.out.println(EntityUtils.toString(response.getEntity()));
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 以回调方式处理返回结果
*
* 处理响应的最简单和最方便的方法是通过使用ResponseHandler的
接口。用户不必担心连接管理的问题。当使用一个
ResponseHandler的时候,无论是否请求执行成功或导致异常,HttpClient将会自动释放连接。
*/
private static void test3(){
CloseableHttpClient client = HttpClients.createDefault();
//==============================================================
ResponseHandler