http工具类

import com.alibaba.nacos.common.utils.MapUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.*;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class HttpsClientUtil {

	public final static int port = 443;

	public final static String SEND_TYPE_JSON = "application/json;charset=UTF-8";

	public final static String SEND_TYPE_XML = "application/xml;charset=UTF-8";
	
	public final static String SEND_TYPE_TEXT = "text/xml;charset=UTF-8";

	private static Logger logger = LoggerFactory.getLogger(HttpsClientUtil.class);

	private static final String HTTP = "http";
	private static final String HTTPS = "https";
	private static SSLConnectionSocketFactory sslsf = null;
	private static PoolingHttpClientConnectionManager cm = null;
	private static SSLContextBuilder builder = null;

	// 初始化https 不做身份验证
	static {
		try {
			builder = new SSLContextBuilder();
			// 全部信任 不做身份鉴定
			builder.loadTrustMaterial(null, new TrustStrategy() {
				@Override
				public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
					return true;
				}
			});
			sslsf = new SSLConnectionSocketFactory(builder.build(),
					new String[] { "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2" }, null, NoopHostnameVerifier.INSTANCE);
			Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
					.register(HTTP, new PlainConnectionSocketFactory()).register(HTTPS, sslsf).build();
			cm = new PoolingHttpClientConnectionManager(registry);
			cm.setMaxTotal(200);// max connection

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public synchronized static CloseableHttpClient getHttpClient() throws Exception {
		CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionManager(cm)
				.setConnectionManagerShared(true).build();

		return httpClient;
	}

	/**
	 * Post 请求
	 * 
	 * @param url
	 * @param sendBody
	 * @param header
	 * @return
	 */
	public static String postForJson(String url, String sendBody, Map<String, String> header) {
		String result = "";
		CloseableHttpClient httpClient = null;
		try {
			httpClient = getHttpClient();
			HttpPost httpPost = new HttpPost(url);
			// 设置头信息
			if (MapUtils.isNotEmpty(header)) {
				for (Map.Entry<String, String> entry : header.entrySet()) {
					httpPost.setHeader(entry.getKey(), entry.getValue());
				}
			}

			// 设置请求实体
			StringEntity entity = new StringEntity(sendBody, "UTF-8");
			entity.setContentType(
					new BasicHeader(org.apache.http.protocol.HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
			if (!StringUtils.isEmpty(entity)) {
				httpPost.setEntity(entity);
			}
			logger.info(MessageFormat.format("Https Request URL: [{0}]", url));
			logger.info(MessageFormat.format("Https Request Headers: [{0}]", header.entrySet()));
			HttpResponse response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity resEntity = response.getEntity();
				result = EntityUtils.toString(resEntity);
			} else {
				readHttpResponse(response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	/**
	 * 
	 * @Description: Post 请求发送XML格式数据
	 * @Title: postForXML
	 * @param: @param url 
	 * @param: @param sendBody
	 * @param: @param header
	 * @param: @return
	 * @return: String
	 * @throws
	 */
	public static String postForXML(String url, String sendBody, Map<String, String> header) {
		String result = "";
		CloseableHttpClient httpClient = null;
		try {

			httpClient = getHttpClient();
			HttpPost httpPost = new HttpPost(url);
			// 设置头信息
			if (MapUtils.isNotEmpty(header)) {
				for (Map.Entry<String, String> entry : header.entrySet()) {
					httpPost.setHeader(entry.getKey(), entry.getValue());
				}
			}

			// 设置请求实体
			StringEntity entity = new StringEntity(sendBody, "UTF-8");
			entity.setContentType(new BasicHeader(org.apache.http.protocol.HTTP.CONTENT_TYPE, SEND_TYPE_XML));
			if (!StringUtils.isEmpty(entity)) {
				httpPost.setEntity(entity);
			}
			logger.info(MessageFormat.format("Https Request URL: [{0}]", url));
			logger.info(MessageFormat.format("Https Request Headers: [{0}]", header.entrySet()));
			HttpResponse response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity resEntity = response.getEntity();
				result = EntityUtils.toString(resEntity, "UTF-8");
			} else {
				readHttpResponse(response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
	/**
	 * Put 请求
	 * 
	 * @param url
	 * @param sendBody
	 * @param header
	 * @return
	 */
	public static String putForJson(String url, String sendBody, Map<String, String> header) {
		String result = "";
		CloseableHttpClient httpClient = null;
		try {
			httpClient = getHttpClient();
			HttpPut httpPut = new HttpPut(url);
			// 设置头信息
			if (MapUtils.isNotEmpty(header)) {
				for (Map.Entry<String, String> entry : header.entrySet()) {
					httpPut.setHeader(entry.getKey(), entry.getValue());
				}
			}
			// 设置请求实体
			StringEntity entity = new StringEntity(sendBody, "UTF-8");
			entity.setContentType(
					new BasicHeader(org.apache.http.protocol.HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
			if (!StringUtils.isEmpty(entity)) {
				httpPut.setEntity(entity);
			}
			logger.info(MessageFormat.format("Https Request URL: [{0}]", url));
			logger.info(MessageFormat.format("Https Request Headers: [{0}]", header.entrySet()));
			HttpResponse response = httpClient.execute(httpPut);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity resEntity = response.getEntity();
				result = EntityUtils.toString(resEntity);
			} else {
				readHttpResponse(response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	/**
	 * Get 请求
	 * 
	 * @param url
	 * @param sendBody
	 * @param header
	 * @return
	 */
	public static String getForJson(String url, String sendBody, Map<String, String> header) {
		String result = "";
		CloseableHttpClient httpClient = null;
		try {
            
			httpClient = getHttpClient();
			HttpGet httpGet = new HttpGet(url);
			// 设置头信息
			if (MapUtils.isNotEmpty(header)) {
				for (Map.Entry<String, String> entry : header.entrySet()) {
					httpGet.setHeader(entry.getKey(), entry.getValue());
				}
			}

			logger.info(MessageFormat.format("Https Request URL: [{0}]", url));
			logger.info(MessageFormat.format("Https Request Headers: [{0}]", header.entrySet()));
			HttpResponse response = httpClient.execute(httpGet);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity resEntity = response.getEntity();
				result = EntityUtils.toString(resEntity);
			} else {
				readHttpResponse(response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	
	
	/**
	 * https 返回状态处理
	 * 
	 * @param response
	 * @return
	 * @throws ParseException
	 * @throws IOException
	 */
	public static String readHttpResponse(HttpResponse response) throws ParseException, IOException {
		StringBuilder builder = new StringBuilder();
		HttpEntity entity = response.getEntity();
		builder.append("status: " + response.getStatusLine());
		builder.append("header: ");
		HeaderIterator iterator = response.headerIterator();
		while (iterator.hasNext()) {
			builder.append("\t" + iterator.next());
		}

		if (!StringUtils.isEmpty(entity)) {
			String result = EntityUtils.toString(entity);
			builder.append("response length: " + result.length());
			builder.append("response content: " + result.replace("\r\n", ""));
		}
		logger.info(MessageFormat.format("Https Response 返回状态结果: {0}", builder.toString()));
		return builder.toString();
	}

	/**
	 * 
	 * @Description: 获取request中的值
	 * @Title: getRequestHeader
	 * @param: @param request
	 * @param: @return
	 * @return: Map<String,String>
	 * @throws
	 */
	public static Map<String, String> getRequestHeader(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		Enumeration<?> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String key = (String) headerNames.nextElement();
			String value = request.getHeader(key);
			map.put(key, value);
		}
		return map;
	}

}


<think> 嗯,用户需要查找HTTPs协议下发送XML请求的案例。根据引用[3]和[4]中的信息,发送XML请求的基本方法与JSON类似,只是需要将请求体格式改为XML并正确设置Content-Type头部。 用户可能正在开发需要与HTTPS服务交互的功能,或者测试某个API接口。引用[3]提到使用Postman工具测试XML接口的示例,引用[4]则展示了XMLHttpRequest发送POST请求的代码框架。这些都可以作为案例的基础。 考虑到用户要求HTTPS协议下的案例,需要特别说明:使用HTTPS只需将URL中的http改为https,并确保服务端支持SSL/TLS。引用[5]提到OpenSSL库提供安全通信支持,这正好解释了HTTPS协议层的工作原理。 我准备给出两个具体案例:一个是Postman工具的操作步骤,另一个是JavaScript代码示例。两者都包含四个关键点:使用POST方法、设置HTTPS URL、配置XML请求体、添加Content-Type头部。这样既覆盖了测试场景也覆盖了开发场景。 针对XML格式的特殊性,要在注意事项中强调标签闭合和编码问题。用户可能遇到的实际问题包括:XML格式错误导致解析失败、缺少命名空间声明、特殊字符未转义等。引用[1]提醒不要生搬硬套,所以案例要突出灵活性。 最后考虑补充相关问题,比如XML与JSON的选择依据、HTTPS证书处理等。用户可能是中级开发者,这些问题能帮助ta理解技术选型。引用[2]的报错案例也值得延伸,可以询问用户是否需要异常处理方案。 </think> 以下是一个完整的**HTTPS协议下发送XML请求的案例**(基于JavaScript和Postman),结合关键步骤与注意事项: --- ### 一、JavaScript代码案例(浏览器端) ```javascript // 1. 创建XMLHttpRequest对象 const xhr = new XMLHttpRequest(); const url = "https://api.example.com/xml-endpoint"; // HTTPS目标地址 // 2. 初始化POST请求 xhr.open("POST", url, true); // 3. 设置请求头(声明XML格式) xhr.setRequestHeader("Content-Type", "application/xml"); // 4. 定义XML请求体 const xmlBody = ` <request> <user> <id>123</id> <name>张三</name> </user> <action>query</action> </request>`; // 5. 发送请求 xhr.send(xmlBody); // 6. 处理响应 xhr.onreadystatechange = function() { if (xhr.readyState === 4) { if (xhr.status >= 200 && xhr.status < 300) { console.log("响应XML数据:", xhr.responseXML); // 解析为XML对象 } else { console.error("请求失败:", xhr.statusText); } } }; ``` --- ### 二、Postman工具案例(测试用) 1. **URL**:`https://api.example.com/xml-endpoint` 2. **Method**:`POST` 3. **Headers**: ```bash Content-Type: application/xml ``` 4. **Body** → 选择`raw` → 输入XML内容: ```xml <request> <param key="api_key">YOUR_HTTPS_KEY</param> <data> <item>value1</item> <item>value2</item> </data> </request> ``` --- ### 三、关键注意事项 1. **HTTPS证书**: - 若为自签名证书,客户端需忽略证书验证(生产环境不推荐) - 浏览器端可能因CORS策略拦截,需服务端配置`Access-Control-Allow-Origin`[^4] 2. **服务端要求**: - 必须支持HTTPS协议(端口通常为443) - 需能解析XML请求体(如Spring框架用`@RequestBody`接收) 3. **XML格式校验**: - 确保XML标签闭合且符合服务端Schema - 特殊字符需转义(如`&` → `&`) --- ### 四、常见问题解决 - **跨域错误**:服务端需响应`Access-Control-Allow-Headers: Content-Type`[^2] - **400 Bad Request**:检查XML结构有效性(可用[XML验证工具](https://www.xmlvalidation.com)) - **SSL/TLS握手失败**:确认服务端支持TLSv1.2+协议[^5] > 案例参考:金融支付接口(如银联HTTPS+XML协议)、SOAP WebService调用[^3][^5] ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值