HttpClient发送post,get请求

13 篇文章 0 订阅
6 篇文章 0 订阅

maven依赖:

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>

 

 

 工具类:

package cn.com.dzqc.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
/**
 * HttpClient工具类
 * 
 * @author 煕寳
 *
 */
public class HttpClientUtil {
	 public static String sendGet(String s) {
	        CloseableHttpClient httpClient = HttpClients.createDefault();
	        StringBuilder entityStringBuilder = null;
	        try {
	            HttpGet get = new HttpGet(s);
	            CloseableHttpResponse httpResponse = null;
	            httpResponse = httpClient.execute(get);
	            try {
	                HttpEntity entity = httpResponse.getEntity();
	                entityStringBuilder = new StringBuilder();
	                if (null != entity) {
	                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"), 8 * 1024);
	                    String line = null;
	                    while ((line = bufferedReader.readLine()) != null) {
	                        entityStringBuilder.append(line + "/n");
	                    }
	                }
	            } finally {
	                httpResponse.close();
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                if (httpClient != null) {
	                    httpClient.close();
	                }
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	        return entityStringBuilder.toString();
	    }

	  /**
	       * 发送POST请求
	       * @param url
	       * @param nameValuePairList
	      * @return JSON或者字符串
	       * @throws Exception
	      */
	      public static String sendPost(String url, List<NameValuePair> nameValuePairList) throws Exception{
	          JSONObject jsonObject = null;
	          CloseableHttpClient client = null;
	          CloseableHttpResponse response = null;
                  String result ="";
	          try{
	              /**
	               *  创建一个httpclient对象,相当于打开一个浏览器
	               */
	              client = HttpClients.createDefault();
	              /**
	              * 创建一个post对象
	               */
	             HttpPost post = new HttpPost(url);
	              /**
	               * 包装成一个Entity对象
	               */
	              StringEntity entity = new UrlEncodedFormEntity(nameValuePairList, "UTF-8");
	              /**
	              * 设置请求的内容
	               */
	              post.setEntity(entity);
	              /**
	               * 设置请求的报文头部的编码
	               */
	             post.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));
	              /**
	               * 设置请求的报文头部的编码
	               */
	              post.setHeader(new BasicHeader("Accept", "text/plain;charset=utf-8"));
	             /**
	               * 执行post请求
	               */
	             response = client.execute(post);
	              /**
	                  * 通过EntityUitls获取返回内容
	                   */
	             result = EntityUtils.toString(response.getEntity(),"UTF-8");
	             
	          }catch (Exception e){
	             e.printStackTrace();
	          }finally {
	            response.close();
	             client.close();
	          }
	          return result;
	      }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值