package com.mes.em.utils; import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocket; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.Consts; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; 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.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.ssl.X509HostnameVerifier; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; /** * @Title: * @Description: * @Author * @Date 2021/12/29 14:41 */ @Slf4j public class HttpClientUtil { /** * 发送HTTP_GET请求 * @see 该方法会自动关闭连接,释放资源 * @param requestURL 请求地址(含参数) * @param decodeCharset 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码 * @return 远程主机响应正文 */ public static String sendGetRequest(String reqURL, String decodeCharset){ //响应长度 long responseLength = 0; //响应内容 String responseContent = null; //创建默认的httpClient实例 HttpClient httpClient = new DefaultHttpClient(); //创建org.apache.http.client.methods.HttpGet HttpGet httpGet = new HttpGet(reqURL); try{ HttpResponse response = httpClient.execute(httpGet); //执行GET请求 HttpEntity entity = response.getEntity(); //获取响应实体 if(null != entity){ responseLength = entity.getContentLength(); responseContent = EntityUtils.toString(entity, decodeCharset==null ? "UTF-8" : decodeCharset); EntityUtils.consume(entity); //Consume response content } System.out.println("请求地址: " + httpGet.getURI()); System.out.println("响应状态: " + response.getStatusLine()); System.out.println("响应长度: " + responseLength); System.out.println("响应内容: " + responseContent); }catch(ClientProtocolException e){ log.debug("该异常通常是协议错误导致,比如构造HttpGet对象时传入的协议不对(将'http'写成'htp')或者服务器端返回的内容不符合HTTP协议要求等,堆栈信息如下", e); }catch(ParseException e){ log.debug(e.getMessage(), e); }catch(IOException e){ log.debug("该异常通常是网络原因引起的,如HTTP服务器未启动等,堆栈信息如下", e); }finally{ //关闭连接,释放资源 httpClient.getConnectionManager().shutdown(); } return responseContent; } /** * 发送HTTP_POST请求 * @see 该方法为<code>sendPostRequest(String,String,boolean,String,String)</code>的简化方法 * @see 该方法在对请求数据的编码和响应数据的解码时,所采用的字符集均为UTF-8 * @see 当<code>isEncoder=true</code>
HttpClient 调用工具类
最新推荐文章于 2024-03-28 10:29:06 发布
本文详细介绍了如何在Java中使用HttpClient进行HTTP和HTTPS请求,包括设置请求头、参数传递、响应处理等关键步骤,帮助开发者更好地理解和运用HttpClient工具类。
摘要由CSDN通过智能技术生成