Apache HttpClient

本文介绍如何使用 Apache HttpClient 4.5.3 版本发送 HTTP GET 和 POST 请求。通过实例演示了如何设置参数并解析响应,为开发者提供了实用的代码示例。

1.下载apache client项目jar包  http://mirrors.tuna.tsinghua.edu.cn/apache//httpcomponents/httpclient/binary/httpcomponents-client-4.5.3-bin.tar.gz

   httpcomponents-client-4.5.3\lib 下面的jar包

package com.service;

import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
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.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.*;

public class ApacheHttpClientUtil {

/**
* 发送post请求
*
* @param url
* @param parameters
*/
public static void httpPost(String url, Map<String, String> parameters) {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
try {
httpclient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);

// post 参数 传递
if (parameters != null) {
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> map : parameters.entrySet()) {
String key = map.getKey().toString();
String value = map.getValue().toString();
list.add(new BasicNameValuePair(key, value));
}
post.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
}

response = httpclient.execute(post);

// 请求发送成功,并得到响应
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//读取服务器返回过来的json字符串数据
String strResult = EntityUtils.toString(response.getEntity());
System.out.println(strResult);
} else {
System.out.println("请求失败");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
if (httpclient != null) {
httpclient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void httpGet(String url) {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
try {
//发送get请求
httpclient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
response = httpclient.execute(request);

/**请求发送成功,并得到响应**/
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
/**读取服务器返回过来的json字符串数据**/
String strResult = EntityUtils.toString(response.getEntity());
System.out.println(strResult);
} else {
System.out.println("请求失败");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
if (httpclient != null) {
httpclient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

 

转载于:https://www.cnblogs.com/lovedaodao/p/7649499.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值