一、HttpClient简介 HttpClient是Apache下的子项目,很著名的另外两个项目Cactus和HTMLunit都使用了HttpClient。 二、HttpClient提供的方法 1、实现了所有的HTTP的方法(get、post、put、head等) 2、支持自动转向 3、支持HTTPS协议 4、支持代理服务器等。 三、基本功能 1、get方法 a、创建HttpClient实例 b、创建某种连接方式的实例,例如:getMethod,在该方法中的构造函数中传入待连接的地址。 c、调用第一步创建好的实例的,execute方法来执行第二步中创建好的Method实例。 d、读取response 3、释放连接 2、post方法 A、调用postMethod方法与getMethod方法类似,除了设置postmethod的实例与getmethod有些不同之外,剩下的步骤都类似。 四、HttpClient post请求的两种方式 (1)直接发送数据

/*
	 * @param data为post请求的数据
	 * @param num 为是否正常执行的状态
	 * @param url 为请求的路径,例如:http://111.111.11.11:12345/TestHttpClientPost
	 * @return
	 */
	public static String getmsgFromRedAnt(String data, int num, String url) {
		String checkResult = "";//执行post之后响应的结果
			HttpClient httpClient = new HttpClient();
			PostMethod postMethod = new PostMethod(url);
			if(num != 0){
				postMethod.setRequestBody(data);//post请求的参数
			}
			try {//执行postMethod
				//执行post请求
				int status = httpClient.executeMethod(postMethod);
				//得到响应结果
				checkResult = postMethod.getResponseBodyAsString();
			} catch (Exception e) {
				e.printStackTrace();
			}finally {//释放连接,不释放会卡住
				postMethod.releaseConnection();
							httpClient.getHttpConnectionManager().closeIdleConnections(0);
			}
		return checkResult;
	}

(2)以实体参数的形式,传递数据

public static String getmsgFromRedAnt(String data, int num, String url) {
			String checkResult = "";//执行post之后响应的结果	
			//创建http post请求
			HttpPost response = new HttpPost(url);
			if(num != 0){//有参数
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				//key参数名称,data参数内容
				params.add(new BasicNameValuePair("key", data));
				try {
					response.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
			//执行post请求
			CloseableHttpResponse httpResonse;
			try {
				httpResonse = (CloseableHttpResponse) httpClient.execute(response);
				//获取结果实体
				HttpEntity entity = httpResonse.getEntity();
				if(entity != null){
					//按照制定编码,转换未结果
					checkResult = EntityUtils.toString(entity);
				}
				EntityUtils.consume(entity);
				//释放链接
				httpResonse.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		return checkResult;
	}

五、相应服务器接受传递的参数 以struts2为例: A、 定义action

		<action name="TestHttpClientPost" class="com.red.ant.httpclient.TestHttpClient" >
					<result name="success" type="dispatcher">
						<param name="location">/resultjson.jsp</param>
					</result>
					<result name="error" type="dispatcher">
						<param name="location">/resultjson.jsp</param>
					</result>
				</action>

B、 两种获取传递参数的方式 (1)数据封装的方式获取,传递的数据

private String request;
	public String getRequest() {
		return request;
	}
	public void setRequest(String request) {
		this.request = request;
	}
System.err.println(request);

(2)requset请求的方式获取传递的参数

HttpServletRequest request = ServletActionContext.getRequest();
String data = (String) request.getParameter("request");
System.err.println(data);

六、代理器

HttpClient中使用的代理服务器非常简单,调用HttpClient中的setProxy方法就可以实现,方法的第一个参数是代理服务器的地址,第二个参数是端口号。另外HttpClient也支持socks代理。 HttpClient.getHostConfiguration().setProxy(hostname,port);

【注意】字符编码 某目标的编码可能出现在两个地方,第一个地方是服务器返回的http头中, 另一个地方是得到的html/xml页面中。 在http头的Content-Type字段可能会包含字符编码信息。 例如:Content-Type: text/html; charset=UTF-8. 可能服务器返回的编码类型是UTF-8,但真正的内容却不是UTF-8编码的,因此需要在另外的地方去得到页面的编码信息; 对于像xml或者html这样的文件,允许作者在页面中直接指定编码的类型 比如:<meta http-equiv=”Content-Type” content=”text/html;charset=gb2312”/>这样的标签; 或者在xml中会有<?xml version=”1.0” encoding=”gb2312”?>这样的标签,需要自己判断到底哪种编码类型是真正的编码。