httpClient调用第三方外部接口

1.添加httpClient架包

2.

package com.zcl.quatrz;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
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.util.EntityUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

public class InterfaceTest {
	
	private static Logger log = LoggerFactory.getLogger(Interface.class);

	 private static RequestConfig requestConfig = null;

	    static
	    {
	        // 设置请求和传输超时时间
	        requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
	    }

	    /**
	     * post请求
	     */
	    public static JSONObject httpPost(String url, String strParam)
	    {
	        // post请求返回结果
	        CloseableHttpClient httpClient = HttpClients.createDefault();
	        JSONObject jsonResult = null;
	        HttpPost httpPost = new HttpPost(url);
	        httpPost.setConfig(requestConfig);
	        try
	        {
	            if (null != strParam)
	            {
	                // 解决中文乱码问题
	                StringEntity entity = new StringEntity(strParam, "utf-8");
	                entity.setContentEncoding("UTF-8");
	                entity.setContentType("application/x-www-form-urlencoded");
	                httpPost.setEntity(entity);
	            }
	            CloseableHttpResponse result = httpClient.execute(httpPost);
	            // 请求发送成功,并得到响应
	            if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
	            {
	                String str = "";
	                try
	                {
	                    // 服务器返回过来的json字符串数据
	                    str = EntityUtils.toString(result.getEntity(), "utf-8");
	                    // 把json字符串转换成json对象
	                    jsonResult = JSONObject.parseObject(str);
	                }
	                catch (Exception e)
	                {
	                    log.error("post请求提交失败:" + url, e);
	                }
	            }
	        } catch (IOException e) {
	            log.error("post请求提交失败:" + url, e);
	        } finally {
	            httpPost.releaseConnection();
	        }
	        return jsonResult;
	    }

	    /**
	     * 发送get请求
	     */
	    public static JSONObject httpGet(String url)
	    {
	        // get请求返回结果
	        JSONObject jsonResult = null;
	        CloseableHttpClient client = HttpClients.createDefault();
	        // 发送get请求
	        HttpGet request = new HttpGet(url);
	        request.setConfig(requestConfig);
	        try
	        {
	            CloseableHttpResponse response = client.execute(request);

	            // 请求发送成功,并得到响应
	            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
	            {
	                // 读取服务器返回过来的json字符串数据
	                HttpEntity entity = response.getEntity();
	                String strResult = EntityUtils.toString(entity, "utf-8");
	                // 把json字符串转换成json对象
	                jsonResult = JSONObject.parseObject(strResult);
	            }
	            else
	            {
	                log.error("get请求提交失败:" + url);
	            }
	        } catch (IOException e) {
	            log.error("get请求提交失败:" + url, e);
	        } finally {
	            request.releaseConnection();
	        }
	        return jsonResult;
	    }
	    
	    @Test
	    public void testDemo(){
	    	JSONObject json =Interface.httpPost("http://localhost:8080/sysUser/getQuery","login=admin&pw=111111");
			System.out.println(json);
	    }
}

猜你喜欢

转载自blog.csdn.net/zzwforbid_404/article/details/81288093