httpClient 发送 http请求


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
//import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
//import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;

public class HttpClientUtil {

    /**
     * @param url    http请求地址
     * @param content    请求实体entity内容
     * @param contentType    Content-Type,默认为text/plain
     * @param headerMap        请求头header键值对
     * @return    响应内容
     * @throws Exception
     */
    public String post(String url,String content,String contentType,Map<String,String> headerMap) throws Exception {
        if (null == content||null == url ) {
            return null;
        }
        if(null==contentType){
            contentType = "text/plain";
        }

        //创建请求

        HttpPost httpPost = new HttpPost(url);//创建post请求

//        HttpPut httpPut = new HttpPut(url);//创建put请求

//        HttpGet httpGet = new HttpGet(url);//创建get请求

        
        if(null!=headerMap){
            Set<Entry<String, String>> set = headerMap.entrySet();
            Iterator<Entry<String, String>> it = set.iterator();
            while(it.hasNext()){
                Entry<String,String> e = (Entry<String,String>)it.next();
                //设置请求头
                httpPost.setHeader(e.getKey(), e.getValue());
            }
        }
        //设置实体
        StringEntity se = new StringEntity(content,ContentType.create(contentType, "UTF-8"));
        httpPost.setEntity(se);
        //创建http客户端
        HttpClient httpclient = HttpClients.createDefault();
        //执行发送,并接收响应
        HttpResponse res = httpclient.execute(httpPost);
        HttpEntity entity = res.getEntity();
        InputStream in = entity.getContent();
        //取出流中的内容
        StringBuffer sb = new StringBuffer();
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        }finally{
            if(in!=null){
                in.close();
            }
        }
        
        return sb.toString();
    }


    public static void main(String[] args) throws Exception {
        Map<String,String> headerMap = new HashMap<String,String>();
        headerMap.put("Authorization", "BASIC ***");
        String content = "{\"mobile\":\"15868800***\",\"content\":\"您正在进行手机校验,验证码是845682,请及时输入。祝您生活愉快!\"}";
        System.out.println(content);
        String contentType = "application/json";

        String url = "http://a***.webpower***.cn/sms/rest/v1/sms";

        System.out.println(new HttpClientUtil().post(url, content, contentType, headerMap));
    }


}


需要的jar包:

commons-logging.jar

httpclient-4.3.jar

httpcore-4.3.jar


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpClient是一个开源的HTTP客户端库,用于发送HTTP请求和处理HTTP响应。它提供了简单易用的API,可以发送GET请求以及其他类型的请求。 要发送GET请求,首先需要创建一个HttpClient对象。然后,创建一个HttpGet对象,并设置请求的URL。最后,使用HttpClient对象执行HttpGet请求,并处理返回的响应。 以下是使用HttpClient发送GET请求的示例代码: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); String url = "http://example.com/api/data"; HttpGet httpGet = new HttpGet(url); try { HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { String responseBody = EntityUtils.toString(entity); System.out.println(responseBody); } } catch (Exception e) { e.printStackTrace(); } } } ``` 以上代码中,我们首先创建了一个HttpClient对象,然后创建了一个HttpGet对象,并设置了请求的URL。接下来,使用HttpClient对象的execute方法执行HttpGet请求,并获取返回的HttpResponse对象。通过HttpResponse对象可以获取响应的实体(HttpEntity),并将其转换为字符串进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值