httpclient的Get请求个Post请求

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>

</dependency>

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.client.utils.URIBuilder;
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.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class HttpClient {
    public static String doPost(String url, Map<String, String> map, String charset) {
        CloseableHttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try {
            httpClient = HttpClients.createDefault();
            httpPost = new HttpPost(url);
            //设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
            }
            if (list.size() > 0) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    public static String doGet(String url) {
        //获取到code,通过token,获取userid
        CloseableHttpResponse response = null;
        CloseableHttpClient httpclient = null;
        String content = "";
        try {
            // 创建Httpclient对象
            httpclient = HttpClients.createDefault();

            // 定义请求的参数
            URI uri = new URIBuilder(url).build();

            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);

            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                // 获取服务端,响应的数据
                content = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content;
    }
}
String url = "url";
            Map<String,String> params = new HashMap<String,String>();
            params.put("key",vauel);
            String s = HttpClient.doPost(url, params, "UTF-8");
String s = HttpClient.doGet(url);
        System.out.println(s);

超级无敌工具类,掉接口必备

如果接口需要你传递json,!!!!

public static String doPost2(String url, Map<String, String> map, String charset) throws Exception{
        HttpPost httpPost = new HttpPost(url);
                 CloseableHttpClient client = HttpClients.createDefault();
                 String respContent = null;

         //        json方式
                 JSONObject jsonParam = new JSONObject();
                 jsonParam.put("user_ticket", map.get("user_ticket"));
                 StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
                 entity.setContentEncoding("UTF-8");
                 entity.setContentType("application/json");
                 httpPost.setEntity(entity);
         //        表单方式
                HttpResponse resp = client.execute(httpPost);
                if(resp.getStatusLine().getStatusCode() == 200) {
                        HttpEntity he = resp.getEntity();
                        respContent = EntityUtils.toString(he,"UTF-8");
            }
                return respContent;
    }




猜你喜欢

转载自blog.csdn.net/weixin_39819191/article/details/79820284