http://blog.youkuaiyun.com/jadyer/article/details/7615830
- package com.jadyer.util;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.ParseException;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.util.EntityUtils;
- /**
- * 使用HttpClient模拟HTTP访问
- * @see ===============================================================================================================================
- * @see HttpClient代表了一个HTTP客户端,HttpClient接口中定义了大多数基本的HTTP请求执行行为
- * @see HttpEntity是发送或接收消息的载体,它可以通过HttpResponse.getEntity()获取到
- * @see HttpConnection代表了一个HTTP连接
- * @see ===============================================================================================================================
- * @author http://blog.youkuaiyun.com/jadyer
- * @editor Feb 1, 2012 3:02:27 PM
- */
- public class HttpClientUtil {
- public static void main(String[] args) throws Exception{
- //String requestUrl = "http://123.125.97.xxx:8804/ecpaycus/test/JFB_web_visit.jsp?phoneNo=18600000001";
- //System.out.println(sendGetRequest(requestUrl));
- String requestUrl = "http://127.0.0.1:8088/test/web/userac" ;
- System.out.println(sendPostRequest(requestUrl));
- }
- /**
- * 发送GET请求
- * @param requestUrl 请求的地址(含参数)
- * @return 响应内容
- */
- @SuppressWarnings ( "finally" )
- public static String sendGetRequest(String requestUrl) {
- long responseLength = 0 ; //响应长度
- String responseContent = null ; //响应内容
- HttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例
- HttpGet httpGet = new HttpGet(requestUrl); //创建HttpGet
- try {
- HttpResponse response = httpClient.execute(httpGet); //执行GET请求
- HttpEntity entity = response.getEntity(); //获取响应实体
- if ( null != entity) {
- responseLength = entity.getContentLength();
- responseContent = EntityUtils.toString(entity, "UTF-8" );
- EntityUtils.consume(entity); //Consume response content
- }
- System.out.println("请求地址: " + httpGet.getURI());
- System.out.println("响应状态: " + response.getStatusLine());
- System.out.println("响应长度: " + responseLength);
- System.out.println("响应内容: " + responseContent);
- } catch (ClientProtocolException e) {
- //该异常通常是协议错误导致
- //比如构造HttpGet对象时传入的协议不对(将"http"写成"htp")或者服务器端返回的内容不符合HTTP协议要求等
- e.printStackTrace();
- } catch (ParseException e) {
- e.printStackTrace();
- } catch (IOException e) {
- //该异常通常是网络原因引起的,如HTTP服务器未启动等
- e.printStackTrace();
- } finally {
- httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源
- return responseContent;
- }
- }
- /**
- * 发送带有表单参数的POST请求
- * @param requestUrl 请求的地址(不含参数)
- * @return 响应内容
- */
- @SuppressWarnings ( "finally" )
- public static String sendPostRequest(String requestUrl) {
- long responseLength = 0 ; //响应长度
- String responseContent = null ; //响应内容
- HttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例
- HttpPost httpPost = new HttpPost(requestUrl); //创建HttpPost
- List<NameValuePair> formParams = new ArrayList<NameValuePair>(); //创建参数队列
- formParams.add(new BasicNameValuePair( "username" , "jadyer" ));
- formParams.add(new BasicNameValuePair( "password" , "hongyu" ));
- try {
- httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8" ));
- HttpResponse response = httpClient.execute(httpPost); //执行POST请求
- HttpEntity entity = response.getEntity(); //获取响应实体
- if ( null != entity) {
- responseLength = entity.getContentLength();
- responseContent = EntityUtils.toString(entity, "UTF-8" );
- EntityUtils.consume(entity); //Consume response content
- }
- System.out.println("请求地址: " + httpPost.getURI());
- System.out.println("响应状态: " + response.getStatusLine());
- System.out.println("响应长度: " + responseLength);
- System.out.println("响应内容: " + responseContent);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (ParseException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源
- return responseContent;
- }
- }
-
}