HttpClient发起post、get请求

来源:互联网 发布:linux启动软件命令 编辑:程序博客网 时间:2024/04/30 00:28

参考文章:

http://blog.csdn.net/wangpeng047/article/details/19624529

http://blog.csdn.net/zknxx/article/details/51598852

一、所需jar包

HttpClient 是apache 组织下面的一个用于处理HTTP 请求和响应的开源工具。所需jar包:

commons-logging-1.1.1.jar、httpclient-4.5.2.jar、httpcore-4.4.4.jar

二、使用方法

使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。

1. 创建HttpClient对象。

2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。

3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)【过时,不推荐使用】方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。对于HttpGet对象,可通过拼接url来添加请求参数。

4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。

5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;

调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。

6. 释放连接。无论执行方法是否成功,都必须释放连接

三、示例

package com.learn.http;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.NameValuePair;import org.apache.http.ParseException;import org.apache.http.client.ClientProtocolException;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.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;public class HttpSend {public static void main(String[] args) {// post请求百度地址,返回404new HttpSend().post("http://www.baidu.com");new HttpSend().get("http://www.baidu.com");}/** * 发送 post请求访问本地应用并根据传递参数不同返回不同结果 */public void post(String url) {// 创建默认的httpClient实例.CloseableHttpClient httpclient = HttpClients.createDefault();// 创建httppostHttpPost httppost = new HttpPost(url);// 创建参数队列List<NameValuePair> formparams = new ArrayList<NameValuePair>();// 添加参数// formparams.add(new BasicNameValuePair("gg", "1"));UrlEncodedFormEntity uefEntity;try {//传入空参数,创建实体对象uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");//设置post对象的实体对象httppost.setEntity(uefEntity);System.out.println("executing post request " + httppost.getURI());//执行post请求CloseableHttpResponse response = httpclient.execute(httppost);try {//获取请求结果对象HttpEntity entity = response.getEntity();if (entity != null) {System.out.println("--------------------------------------");System.out.println("Response content: " + EntityUtils.toString(entity));System.out.println("--------------------------------------");}} finally {response.close();}} catch (ClientProtocolException e) {e.printStackTrace();} catch (UnsupportedEncodingException e1) {e1.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {// 关闭连接,释放资源try {httpclient.close();} catch (IOException e) {e.printStackTrace();}}}/** * 发送 get请求 */public void get(String url) {// 创建默认的httpClient实例.CloseableHttpClient httpclient = HttpClients.createDefault();try {// 封装请求参数List<NameValuePair> params = new ArrayList<>();params.add(new BasicNameValuePair("cityEname", "henan"));//转换为键值对String str = EntityUtils.toString(new UrlEncodedFormEntity(params, "UTF-8"));System.out.println(str);//创建带参数get请求(示例)HttpGet httpGet = new HttpGet(url+"?"+str);// 创建不带参数get请求HttpGet httpget = new HttpGet(url);System.out.println("executing get request " + httpget.getURI());// 执行get请求.CloseableHttpResponse response = httpclient.execute(httpget);try {// 获取响应实体HttpEntity entity = response.getEntity();System.out.println("--------------------------------------");// 打印响应状态System.out.println(response.getStatusLine());if (entity != null) {// 打印响应内容长度System.out.println("Response content length: " + entity.getContentLength());// 打印响应内容System.out.println("Response content: " + EntityUtils.toString(entity));}System.out.println("------------------------------------");} finally {response.close();}} catch (ClientProtocolException e) {e.printStackTrace();} catch (ParseException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {// 关闭连接,释放资源try {httpclient.close();} catch (IOException e) {e.printStackTrace();}}}}



0 1