HttpClient post 和get 带参数请求
HttpClient post 和get 带参数请求
指尖幸福 发表于6个月前
HttpClient post 和get 带参数请求
  • 发表于 6个月前
  • 阅读 19
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

package com.spf;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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;

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

/**
 * Created by SPF
 */
public class HttpClientUtils {

    public static String post(String url, Map<String, Object> map) {
        CloseableHttpClient client =null;
        HttpPost post = null;
        CloseableHttpResponse response = null;
        String result = null;
        try {
            client = HttpClients.createDefault();
            post = new HttpPost(url);

            //设置超时时间
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(2000) //设置链接超时时间
                    .setSocketTimeout(8000)//设置读取超时时间
                    .build();
            post.setConfig(requestConfig);

            //设置请求头信息,模拟浏览器请求
            post.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36");

            //设置参数
            if(map != null && !map.isEmpty()) {
                List<NameValuePair> list = new ArrayList<NameValuePair>();
                Iterator iterator = map.entrySet().iterator();
                while(iterator.hasNext()){
                    Map.Entry<String,Object> elem = (Map.Entry<String, Object>) iterator.next();
                    list.add(new BasicNameValuePair(elem.getKey(),elem.getValue().toString()));
                }
                if(list.size() > 0){
                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"UTF-8");
                    post.setEntity(entity);
                }
            }
            //执行请求
            response = client.execute(post);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,"UTF-8");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    
public static String get(String url, Map<String, String> map) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        InputStream is = null;
        //封装请求参数
        List<NameValuePair> params = Lists.newArrayList();
        Iterator iterator = map.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry<String,Object> elem = (Map.Entry<String, Object>) iterator.next();
            if (elem.getValue() == null) {
                continue;
            }
            params.add(new BasicNameValuePair(elem.getKey(),elem.getValue().toString()));
        }
        String str = "";
        String body = null;
        String str2 = "";
        try {
            //转换为键值对
            str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
            System.out.println(str);
            //创建Get请求
            HttpGet httpGet = new HttpGet(url+"?"+str);
            //执行Get请求,
            response = httpClient.execute(httpGet);
            //得到响应体
            HttpEntity entity = response.getEntity();
            str2 = EntityUtils.toString(entity,"UTF-8");
//            if(entity != null){
//                is = entity.getContent();
//                //转换为字节输入流
//                BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
//                while((body=br.readLine()) != null){
//                    System.out.println(body);
//                }
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }  finally{
            //关闭输入流,释放资源
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //消耗实体内容
            if(response != null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //关闭相应 丢弃http连接
            if(httpClient != null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return str2;
    }
}
 
附:设置代理IP
 
//设置代理IP HttpHost httpHost = new HttpHost("115.197.169.12",808); RequestConfig requestConfig = RequestConfig.custom().setProxy(httpHost).build(); post.setConfig(requestConfig);
共有 人打赏支持
粉丝 0
博文 7
码字总数 3006
×
指尖幸福
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: