Http 设置超时 + X509协议

本文详细探讨了如何在HTTP请求中设置超时以及使用X509协议进行安全通信。分别介绍了将URL和PARAMS分开发送、放在头部一起发送的POST方法,同时提及了默认的HTTP操作以及GET请求的应用。
摘要由CSDN通过智能技术生成

POST

URL, PARAMS 分开发送

public class HttpClient {

	private static final AllowAllHostnameVerifier HOSTNAME_VERIFIER = new AllowAllHostnameVerifier();

	private static X509TrustManager xtm = new X509TrustManager() {
		@Override
		public void checkClientTrusted(X509Certificate[] chain, String authType) {
		}

		@Override
		public void checkServerTrusted(X509Certificate[] chain, String authType) {
		}

		@Override
		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}
	};

	private static X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };

	static String paths = "";
	static Map<String, String> paramss = null;
	static String encodings = "";
	// static String bakold = "";
	static String RetBak = "";
	static boolean isRe = true;
	static int isStop = 0;

	// 增加捕获超时异常
	public static String sendPOSTRequestForInputStream(String path, Map<String, String> params, String encoding) throws ConnectTimeoutException {

		paths = path;
		paramss = params;
		encodings = encoding;
		// bakold = "";
		RetBak = "";
		isRe = true;
		isStop = 0;

		class acceptThread extends Thread {
			Map<String, String> paramsa = null;

			public acceptThread(Map<String, String> params) {
				this.paramsa = params;
			}

			InputStream in = null;
			private HttpsURLConnection conn = null;

			@Override
			public void run() {
				String bakn = "";
				try {

					StringBuilder entityBuilder = new StringBuilder("");
					if (paramsa != null && !paramss.isEmpty()) {
						for (Map.Entry<String, String> entry : paramss.entrySet()) {
							entityBuilder.append(entry.getKey()).append('=');
							entityBuilder.append(URLEncoder.encode(entry.getValue(), encodings));
							entityBuilder.append('&');
						}
						entityBuilder.deleteCharAt(entityBuilder.length() - 1);
					}

					byte[] entity = entityBuilder.toString().getBytes();
					URL url = new URL(paths);
					conn = (HttpsURLConnection) url.openConnection();
					if (conn instanceof HttpsURLConnection) {
						// Trust all certificates
						SSLContext context = SSLContext.getInstance("TLS");
						context.init(new KeyManager[0], xtmArray, new SecureRandom());
						SSLSocketFactory socketFactory = context.getS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值