一、HttpClient
(一)HttpClient 客户端
1、HttpClient 是 apache 的开源,需要引入两个包:httpclient-4.2.4.jar 和 httpcore-4.2.2.jar。
/**
* HttpClien 的客户端访问
*/
private void httpClientVisit() {
String clientResponse = "";
try {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(mUrl);
//不是表单数据提交,这边使用 StringEntity 即可
//UrlEncodedFormEntity等都是 HttpEntity 接口的实现类
StringEntity entity = new StringEntity(mRequestXml, "UTF-8");//编码
entity.setContentType("text/xml");
request.setEntity(entity);
// 发送请求
HttpResponse response = client.execute(request);
org.apache.http.HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// EntityUtils.toString 如果不指定编码,EntityUtils默认会使用ISO_8859_1进行编码
clientResponse = EntityUtils.toString(httpEntity, "UTF-8");// 记得设置编码或者如下
// clientResponse = new String(EntityUtils.toString(httpEntity).getBytes("ISO_8859_1"), "UTF-8");
}
if (clientResponse == null || "".equals(clientResponse)) {
System.err.println("clientResponse is null or empty.");
}
System.out.println(clientResponse);
} catch (Exception e) {
e.prin