//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.xiaocaimi.framework.common.http;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClient {
public HttpClient() {
}
public static String sendPostDataByMap(String url, Map<String, String> map, String encoding) throws ClientProtocolException, IOException {
String result = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList();
if (map != null) {
Iterator var7 = map.entrySet().iterator();
while(var7.hasNext()) {
Entry<String, String> entry = (Entry)var7.next();
nameValuePairs.add(new BasicNameValuePair((String)entry.getKey(), (String)entry.getValue()));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding));
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
CloseableHttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}
response.close();
return result;
}
public static String sendPostDataByJson(String url, String json, String encoding) throws ClientProtocolException, IOException {
String result = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
stringEntity.setContentEncoding("utf-8");
httpPost.setEntity(stringEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}
response.close();
return result;
}
public static String sendGetData(String url, String encoding) throws ClientProtocolException, IOException {
String result = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Content-type", "application/json");
CloseableHttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}
response.close();
return result;
}
public static void testSendPostDataByJson() throws ClientProtocolException, IOException {
String url = "http://apitest.51xcm.cn/admin/xiaocaimi/receiveJDLoanData";
new HashMap();
String data = "[{'id':'1234567','type':'c','firstLoan':'true','institutionName':'及贷','productName':'及贷','phone':'196797c6ed13ecaa3b9f3ccec0874792','realLoanMoney':'100000','userName':'','idCardNo':'','applyLoanDate':1539681080000,'lendingTime':1539681080000,'planLastRepayTime':'1549681080000','remark':'aa'}]";
String body = sendPostDataByJson(url, data, "utf-8");
System.out.println("响应结果:" + body);
}
public static void main(String[] args) {
try {
testSendPostDataByJson();
} catch (Exception var2) {
;
}
}
}
Java调用第三方api---sendPostDataByMap
最新推荐文章于 2020-10-15 13:40:46 发布