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];
}
}