麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

HttpClient中GET、POST方法示例,支持 https

2019-11-11 06:37:05
字體:
來源:轉載
供稿:網友

HttpClient中GET、POST方法示例,支持 HTTPS

import java.io.IOException;import java.security.GeneralSecurityException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttPResponse;import org.apache.http.HttpStatus;import org.apache.http.HttpVersion;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.params.BasicHttpParams;import org.apache.http.params.CoreProtocolPNames;import org.apache.http.params.HttpParams;import org.apache.http.util.EntityUtils;import net.sf.json.JSONObject;public class HttpUtil {		public static String connectUrlNotCheckSsl(String url, String requestMethod, JSONObject param) 			throws RuntimeException, GeneralSecurityException, IOException {		if (StringUtil.isNull(url)){			throw new RuntimeException("請求地址不能為空!");		}		if (url.endsWith("/") || url.endsWith("?") || url.endsWith("&")) {			url = url.substring(0, url.length() - 1);		}		if (url.length() <= 4) {			url = "http://" + url;		}		if (!"http".equalsIgnoreCase(url.substring(0, 4))) {			url = "http://" + url;		}				HttpParams httpParams = new BasicHttpParams();		httpParams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);		httpParams.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");		httpParams.setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");				HttpClient httpClient = null;		if ("http:".equalsIgnoreCase(url.substring(0, 5))) {			httpClient = new DefaultHttpClient(httpParams);		} else {			httpClient = new SSLDefaultHttpClient(httpParams);		}		HttpResponse httpResponse = null;		if (StringUtil.isNotNull(requestMethod) && "post".equalsIgnoreCase(requestMethod)) { //post請求			HttpPost httpPost = new HttpPost(url);			if (param != null && !param.isEmpty()) {				List<NameValuePair> postParam = new ArrayList<NameValuePair>();				Iterator<?> it = param.keys();				while (it.hasNext()) {					String key = (String) it.next();					String value = param.getString(key);					postParam.add(new BasicNameValuePair(key, value));				}				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParam, "UTF-8");				httpPost.setEntity(entity);			}			httpResponse = httpClient.execute(httpPost);		} else { //get請求			if (param != null && !param.isEmpty()) {				StringBuffer sb = new StringBuffer();				Iterator<?> it = param.keys();				while (it.hasNext()) {					String key = (String) it.next();					String value = param.getString(key);					if (sb.length() <= 0) {						sb.append("?");					} else {						sb.append("&");					}					sb.append(key).append("=").append(value);				}				url += sb.toString();			}			HttpGet httpGet = new HttpGet(url);			httpResponse = httpClient.execute(httpGet);		}		int responseCode = httpResponse.getStatusLine().getStatusCode();		if (responseCode != HttpStatus.SC_OK){			throw new RuntimeException("請求響應失敗,響應碼:" + responseCode + "!");		}		HttpEntity httpEntity = httpResponse.getEntity();		return (httpEntity == null) ? null : EntityUtils.toString(httpEntity, "UTF-8");	}}import java.security.GeneralSecurityException;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;import org.apache.http.conn.ClientConnectionManager;import org.apache.http.conn.scheme.Scheme;import org.apache.http.conn.scheme.SchemeRegistry;import org.apache.http.conn.ssl.SSLSocketFactory;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.params.HttpParams;public class SSLDefaultHttpClient extends DefaultHttpClient {	public SSLDefaultHttpClient(HttpParams httpParams) throws GeneralSecurityException {		super(httpParams);		SSLContext ctx = SSLContext.getInstance("TLS");		X509TrustManager tm = new X509TrustManager() {			@Override			public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {			}			@Override			public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {			}			@Override			public X509Certificate[] getAcceptedIssuers() {				return null;			}		};		ctx.init(null, new TrustManager[] { tm }, null);		SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);		ClientConnectionManager ccm = this.getConnectionManager();		SchemeRegistry sr = ccm.getSchemeRegistry();		sr.register(new Scheme("https", 443, ssf));	}}


上一篇:LintCode 407:Plus One

下一篇:引用類型剖析

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久精品视频首页 | 影视免费观看 | 黄色免费电影网址 | 久草免费资源视频 | 成人在线视频播放 | sese综合 | 午夜精品一区二区三区免费 | 日韩精品一区二区三区中文 | 久久99国产视频 | 亚洲国产网址 | 欧美一级黄色片免费观看 | 久久久久一本一区二区青青蜜月 | 蜜桃91丨九色丨蝌蚪91桃色 | 中文字幕在线网站 | 一区国产在线 | 色猫av| 成人视屏网站 | 欧美91看片特黄aaaa | 黄色免费在线电影 | 91精品久久久久久 | 美女羞羞视频在线观看 | 国产区二区 | 成人9禁啪啪无遮挡免费 | 精品一区二区三区在线播放 | 最新在线中文字幕 | 茄子福利视频 | 亚洲成人在线视频网 | 高清做爰免费无遮网站挡 | 激情久久一区二区 | 91看片王| 毛片免费大全短视频 | 在线中文字幕观看 | a黄网站 | 久精品久久 | 少妇一级淫片高潮流水电影 | 国产91在线高潮白浆在线观看 | 草久影视 | 国产一区二区三区高清 | 免费在线观看国产精品 | 国产成人精品网站 | 一区二区三区四区高清视频 |