httpsClient


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.*;
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.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class HttpClientUtil {
/***
* 提交https或http Post请求
* @param url 请求url
* @param parameters 请求参数map
* @param httpEnum http或https
* @param port 请求端口 默认443
* @throws IOException
*/
public static void doPost(String url, Map<String,Object> parameters, Enum httpEnum, int port) throws IOException {
if(port == 0){
port = 443;
}
HttpClient httpClient = new DefaultHttpClient();
if(httpEnum !=null && httpEnum.equals("HTTPS")){
        /**
* 调用https,创建https连接
*/
       SSLSocketFactory sslSocketFactory = new SSLSocketFactory(enableSSL());
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);//
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme(
"https", sslSocketFactory, port
));
}
HttpResponse response = null;
try {
HttpPost post = new HttpPost(url);
post.setHeader("contentType", "application/json;charset=UTF-8");
post.setHeader("Accept", "application/json");

/*
//模拟表单提交
List<NameValuePair> pairList = new ArrayList<>();
Set<Map.Entry<String, Object>> entrySet = parameters.entrySet();
Iterator<Map.Entry<String, Object>> entryIterator = entrySet.iterator();
while(entryIterator.hasNext()){
String key = entryIterator.next().toString();
String value = parameters.get(key).toString();
pairList.add(new BasicNameValuePair(key, value));
}
post.setEntity(new UrlEncodedFormEntity(pairList));
*/

//提交json串
String parameter = JSONObject.fromObject(parameters).toString();
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(new StringEntity(parameter));
post.setEntity(bufferedHttpEntity);
response = httpClient.execute(post);
} catch (IOException e) {
e.printStackTrace();
}
if (response.getStatusLine().getStatusCode() == 200) {
System.out.println(getResponseAsString(response));
}else {
System.out.println(getResponseAsString(response));
}
}

public static void main(String[] args) {
Map<String,Object> parameters = new HashMap<String, Object>();
parameters.put("username","dysec");
parameters.put("password","venustech60");
try {
doPost("https://xxxx",parameters,EEE.HTTPS, 443);
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 将response响应消息转换为字符串
* @param response
* @return
* @throws IOException
*/
public static String getResponseAsString(HttpResponse response) throws IOException {
String result = "";
HttpEntity resEntity = response.getEntity();
InputStreamReader reader = new InputStreamReader(resEntity.getContent());
char[] buff = new char[1024];
int length = 0;
while ((length = reader.read(buff)) != -1) {
result += new String(buff, 0, length);
}
return result;
}
/**
* 创建自动接收证书实例
* @return
*/
public static SSLContext enableSSL() {
SSLContext sslctx = null;
try {
sslctx = SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
sslctx.init(null, new X509TrustManager[]{new MyTrustManager()
}, null);
} catch (KeyManagementException e) {
e.printStackTrace();
}
return sslctx;
}
}
/**
* 自定义重写自动接收所有证书
*/
class MyTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}

@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}

public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}

转载于:https://www.cnblogs.com/xmcainiao/p/6913608.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值