Java access third-party interface

1. Tools used


import com.alibaba.fastjson.JSONObject;
import com.sq.fms.common.CtlStatus;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.client.methods.RequestBuilder;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.MimeTypeUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpUtil {
    
    
    private final static Logger log = LoggerFactory.getLogger(HttpUtil.class);
    public static int TIME_OUT = 1000 * 12;// 12秒
    private static CloseableHttpClient httpclient = HttpClients.createDefault();
   
    /**
     * 执行一个HTTP GET请求,返回请求响应的HTML
     *
     * @param url     请求的URL地址
     * @param params  请求的查询参数,可以为null
     * @param charset 字符集 Consts.UTF_8
     * @return 返回请求响应的HTML
     */

    public static String doGet(String url, Map<String, String> params, Charset charset, int timeout) throws Exception {
    
    
        CloseableHttpResponse response = null;
        try {
    
    
            timeout = timeout > 0 ? timeout : TIME_OUT;
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
            httpclient = HttpClients.createDefault();
            List<NameValuePair> valuePairs = new ArrayList<NameValuePair>(params.size());
            for (Map.Entry<String, String> entry : params.entrySet()) {
    
    
                NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()));
                valuePairs.add(nameValuePair);
            }
            RequestBuilder requestBuilder = RequestBuilder.get().setConfig(requestConfig).setUri(url)
                    .addParameters(valuePairs.toArray(new NameValuePair[valuePairs.size()]));
            response = httpclient.execute(requestBuilder.build());
            HttpEntity entity = response.getEntity();
            String respContent = EntityUtils.toString(entity, charset).trim();
            return respContent;

        } catch (Exception e) {
    
    
            throw new Exception(e);
        } finally {
    
    
            if (response != null) {
    
    
                response.close();
            }
        }
    }

    /**
     * 执行一个HTTP GET请求,返回请求响应的HTML
     *
     * @param url         请求的URL地址,可以带有get参数,如果参数中有汉字则必须将参数写在queryString中并指定编码
     * @param queryString 请求的查询参数,可以为null
     * @param charset     字符集
     * @param pretty      是否美化
     * @return 返回请求响应的HTML
     */
    public static String doGet(String url, String queryString, String charset, boolean pretty) {
    
    
        StringBuffer response = new StringBuffer();
        HttpClient client = new HttpClient();
        // 链接超时(单位毫秒)
        client.getHttpConnectionManager().getParams().setConnectionTimeout(TIME_OUT);
        // 读取超时(单位毫秒)
        client.getHttpConnectionManager().getParams().setSoTimeout(TIME_OUT);

        HttpMethod method = new GetMethod(url);
        // URL中可能带有参数,需要拼接
        String preParam = method.getQueryString();
        try {
    
    
            if (StringUtils.isNotBlank(queryString)) {
    
    
                // 对get请求参数做了http请求默认编码汉字编码后,就成为%式样的字符串
                String encodeUrl = URIUtil.encodeQuery(queryString);
                if (StringUtils.isEmpty(preParam)) {
    
    
                    method.setQueryString(encodeUrl);
                } else {
    
    
                    // 拼接参数
                    method.setQueryString(preParam + "&" + encodeUrl);
                }
            }
            if (log.isDebugEnabled()) {
    
    
                log.debug(url);
                log.debug(method.getQueryString());
            }

            client.executeMethod(method);
            if (method.getStatusCode() == HttpStatus.SC_OK) {
    
    
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(method.getResponseBodyAsStream(), charset));
                String line;
                while ((line = reader.readLine()) != null) {
    
    
                    if (pretty)
                        response.append(line).append(System.getProperty("line.separator"));
                    else
                        response.append(line);
                }
                reader.close();
            }
        } catch (URIException e) {
    
    
            log.error("执行HTTP Get请求时,编码查询字符串“" + queryString + "”发生异常!", e);
        } catch (IOException e) {
    
    
            log.error("执行HTTP Get请求" + url + "时,发生异常!", e);
        } finally {
    
    
            method.releaseConnection();
        }
        return response.toString();
    }


}

2. Access interface

// 参数1:访问的地址url,参数2:访问时携带的参数,可以为null,参数3:字符集,参数4:是否美化。
 public string getDatas(){
    
    
        String resStr = HttpUtil.doGet("http://www.XXXX/index.html?", "name=zhangsan&password=zhangsan", Consts.UTF_8.toString(), true);
        System.out.println("网页数据---》" + resStr);
         return resStr ;
    }

You can get the html of the web page you visited.

Guess you like

Origin blog.csdn.net/qq_42182034/article/details/110200243