Java网络通信之HttpClient

HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。在Android系统中内置了HttpClient。Android下可以试试Square的OkHttp

http://hc.apache.org/httpcomponents-client-ga/index.html

版本:httpclient-4.2.jar


1、基本请求
//创建一个客户端
HttpClient client = new DefaultHttpClient();

//创建一个get方法
HttpGet get = new HttpGet("http://www.baidu.com");

//执行请求
HttpResponse res = client.execute(get);

//获取协议版本・・・「HTTP/1.1」
System.out.println(res.getProtocolVersion());
//获取返回状态码・・・「200」
System.out.println(res.getStatusLine().getStatusCode());
//获取原因短语・・・「OK」
System.out.println(res.getStatusLine().getReasonPhrase());
//获取完整的StatusLine・・・「HTTP/1.1 200 OK」
System.out.println(res.getStatusLine().toString());

//获取返回头部信息
Header[] headers = res.getAllHeaders();
for (Header header : headers) {
	System.out.println(header.getName() + ": " + header.getValue());
}

//获取返回内容
if (res.getEntity() != null) {
	System.out.println(EntityUtils.toString(res.getEntity()));
}

//关闭流
EntityUtils.consume(res.getEntity());

//关闭连接
client.getConnectionManager().shutdown();


2、操作Cookie
//生成Cookie
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie stdCookie = new BasicClientCookie("name", "value");
stdCookie.setVersion(1);
stdCookie.setDomain(".baidu.com");
stdCookie.setPath("/");
stdCookie.setSecure(true);
stdCookie.setAttribute(ClientCookie.VERSION_ATTR, "1");
stdCookie.setAttribute(ClientCookie.DOMAIN_ATTR, ".baidu.com");
cookieStore.addCookie(stdCookie);

DefaultHttpClient client1 = new DefaultHttpClient();
client1.setCookieStore(cookieStore);

client1.execute(new HttpGet("http://www.baidu.com/"));

//获取Cookie
DefaultHttpClient client2 = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.baidu.com/");

HttpResponse res = client2.execute(httpget);

List<Cookie> cookies = client2.getCookieStore().getCookies();
for (Cookie cookie : cookies) {
	System.out.println(cookie.getName());
	System.out.println(cookie.getValue());
}

EntityUtils.consume(res.getEntity());


3、设置代理
//通过连接参数设置代理
DefaultHttpClient client1 = new DefaultHttpClient();

HttpHost proxy1 = new HttpHost("192.168.2.60", 8080, "HTTP");
client1.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy1);

HttpGet get1 = new HttpGet("http://www.facebook.com");
HttpResponse res1 = client1.execute(get1);
if (res1.getEntity() != null) {
	System.out.println(EntityUtils.toString(res1.getEntity()));
}		
EntityUtils.consume(res1.getEntity());

//设置代理认证
//client1.getCredentialsProvider().setCredentials(
//        new AuthScope("localhost", 8080),
//        new UsernamePasswordCredentials("username", "password"));


4、POST数据
//========UrlEncodedFormEntity
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("param1", "value1"));
formparams.add(new BasicNameValuePair("param2", "value2"));
UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(formparams, "UTF-8");

HttpPost post1 = new HttpPost("http://localhost/post1.do");
post1.setEntity(entity1);

new DefaultHttpClient().execute(post1);

//========FileEntity
FileEntity entity2 = new FileEntity(new File("c:\\sample.txt"));
entity2.setContentEncoding("UTF-8");

HttpPost post2 = new HttpPost("http://localhost/post2.do");
post2.setEntity(entity2);

new DefaultHttpClient().execute(post2);

//========MultipartEntity
HttpPost post3 = new HttpPost("http://localhost/post3.do");
File upfile = new File("C:\\test.jpg");
MultipartEntity entity3 = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"));
entity3.addPart("file_name",     new StringBody(upfile.getName()));
entity3.addPart("file_contents", new FileBody(upfile));
post3.setEntity(entity3);

new DefaultHttpClient().execute(post3);


5、URI构建
//方法一
URI uri1 = URIUtils.createURI("http", "www.baidu.com", -1, "/s",
		"wd=rensanning&rsv_bp=0&rsv_spt=3&inputT=1766", null);
HttpGet httpget1 = new HttpGet(uri1);

//http://www.baidu.com/s?wd=rensanning&rsv_bp=0&rsv_spt=3&inputT=1766
System.out.println(httpget1.getURI());

//方法二
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("wd", "rensanning"));
qparams.add(new BasicNameValuePair("rsv_bp", "0"));
qparams.add(new BasicNameValuePair("rsv_spt", "3"));
qparams.add(new BasicNameValuePair("inputT", "1766"));

URI uri2 = URIUtils.createURI("http", "www.baidu.com", -1, "/s",
		URLEncodedUtils.format(qparams, "UTF-8"), null);
HttpGet httpget2 = new HttpGet(uri2);

//http://www.baidu.com/s?wd=rensanning&rsv_bp=0&rsv_spt=3&inputT=1766
System.out.println(httpget2.getURI());


6、使用Scheme
DefaultHttpClient  httpclient = new DefaultHttpClient();

//========将Scheme设置到client中
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();

Scheme httpsScheme = new Scheme("https", 443, socketFactory);
httpclient.getConnectionManager().getSchemeRegistry().register(httpsScheme);

HttpGet httpget = new HttpGet("https://xxx.xxx.xxx");
HttpResponse res =  httpclient.execute(httpget);

System.out.println(EntityUtils.toString(res.getEntity()));

//========使用证书做SSL连接
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream fis = new FileInputStream("c:\\aa.keystore");
try {
	keyStore.load(fis, "password".toCharArray());
} finally {
	fis.close();
}

@SuppressWarnings("unused")
SSLSocketFactory socketFactory2 = new SSLSocketFactory(keyStore);
//......


7、认证
//========BASIC认证
DefaultHttpClient httpclient1 = new DefaultHttpClient();

UsernamePasswordCredentials creds1 = new UsernamePasswordCredentials("userid", "password");
AuthScope auth1 = new AuthScope("localhost", 80);

httpclient1.getCredentialsProvider().setCredentials(auth1, creds1);

HttpGet httpget1 = new HttpGet("http://localhost/auth1");

@SuppressWarnings("unused")
HttpResponse res1 =  httpclient1.execute(httpget1);

//========BASIC认证(HttpContext)
DefaultHttpClient httpclient2 = new DefaultHttpClient();

UsernamePasswordCredentials creds2 = new UsernamePasswordCredentials("admin", "admin");
AuthScope auth2 = new AuthScope("localhost", 80);

httpclient2.getCredentialsProvider().setCredentials(auth2, creds2);

HttpHost targetHost2 = new HttpHost("localhost", 80, "http");

AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost2, basicAuth);

BasicHttpContext localcontext2 = new BasicHttpContext();
localcontext2.setAttribute(ClientContext.AUTH_CACHE, authCache);

HttpGet httpget2 = new HttpGet("http://localhost/auth2");

@SuppressWarnings("unused")
HttpResponse res2 =  httpclient2.execute(httpget2, localcontext2);

//========DIGEST认证
DefaultHttpClient httpclient3 = new DefaultHttpClient();

UsernamePasswordCredentials creds3 = new UsernamePasswordCredentials("admin", "admin");
AuthScope auth3 = new AuthScope("localhost", 80);

httpclient3.getCredentialsProvider().setCredentials(auth3, creds3);

HttpHost targetHost3 = new HttpHost("localhost", 80, "http");

AuthCache authCache3 = new BasicAuthCache();
DigestScheme digestAuth = new DigestScheme();
digestAuth.overrideParamter("realm", "some realm");
digestAuth.overrideParamter("nonce", "whatever");
authCache3.put(targetHost3, digestAuth);

BasicHttpContext localcontext3 = new BasicHttpContext();
localcontext3.setAttribute(ClientContext.AUTH_CACHE, authCache3);

HttpGet httpget3 = new HttpGet("http://localhost/auth3");

@SuppressWarnings("unused")
HttpResponse res3 =  httpclient2.execute(httpget3, localcontext3);

//========NTLM认证
DefaultHttpClient httpclient4 = new DefaultHttpClient();

NTCredentials creds4 = new NTCredentials("user", "pwd", "myworkstation", "microsoft.com");

httpclient4.getCredentialsProvider().setCredentials(AuthScope.ANY, creds4);

HttpHost targetHost4 = new HttpHost("hostname", 80, "http");

HttpContext localcontext4 = new BasicHttpContext();

HttpGet httpget4 = new HttpGet("http://localhost/auth4");

@SuppressWarnings("unused")
HttpResponse res4 = httpclient3.execute(targetHost4, httpget4, localcontext4);


8、连接池
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
//设置连接最大数
cm.setMaxTotal(200);
//设置每个Route的连接最大数
cm.setDefaultMaxPerRoute(20);
//设置指定域的连接最大数
HttpHost localhost = new HttpHost("locahost", 80);
cm.setMaxPerRoute(new HttpRoute(localhost), 50);

HttpGet httpget = new HttpGet("http://localhost/pool");

HttpClient client = new DefaultHttpClient(cm);

@SuppressWarnings("unused")
HttpResponse res = client.execute(httpget);


猜你喜欢

转载自rensanning.iteye.com/blog/1550436