首先要導入對應的jar包:jar包地址:https://pan.baidu.com/s/1LNn5nSW8McYGy1EhsYD9VQjava
import java.util.ArrayList;apache
import java.util.Date;json
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONObject;api
public class httpclient {app
/*入參說明
*
* param url 請求地址
* param jsonObject 請求的json數據
* param encoding 編碼格式
*
* */
public static String jsonPost(String url,JSONObject jsonObject,String encoding){
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String response = null;
try {
StringEntity s = new StringEntity(jsonObject.toString());
s.setContentEncoding(encoding);
s.setContentType("application/json");//發送json數據須要設置contentType
post.setEntity(s);
HttpResponse res = httpclient.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
response = JSONObject.toJSONString(result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
/*入參說明
*
* param url 請求地址
* param map 請求的map數據
* param encoding 編碼格式
*
* */
public static String mapPost(String url, Map<String,Object> map, String encoding){
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
//設置參數
List<NameValuePair> list = new ArrayList<NameValuePair>();
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<String,String> elem = (Map.Entry<String, String>) iterator.next();
list.add(new BasicNameValuePair(elem.getKey(),String.valueOf(elem.getValue())));
}
if(list.size() > 0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,encoding);
httpPost.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,encoding);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
public static void main(String[] args) {
String result = "";
//一個json數據的post請求
//版本號
Integer apiVersion = Constants.APIVERSION;
//時間戳
Long txnDate = new Date().getTime();
//封裝json數據
JSONObject obj = new JSONObject();
obj.put("apiVersion", apiVersion);
obj.put("txnDate", txnDate);
//提交請求
result = jsonPost("請求地址", obj,"utf-8");
System.out.println(result);
post
//當時在測試的時候,返回的參數會出現中文亂碼。這個是由於返回的參數使用了Unicode編碼。因此用json轉一下就能夠了。測試
JSONObject jsonObject=JSONObject.parseObject(result);
result = jsonObject.toJSONString();ui
System.out.println("---------------------------------------------");
//一個map數據的post請求
//版本號
Integer apiVersion2 = Constants.APIVERSION;
//時間戳
Long txnDate2 = new Date().getTime();
//封裝map數據
Map<String, Object> obj2 = new HashMap<String, Object>();
obj2.put("apiVersion2", apiVersion2);
obj2.put("txnDate2", txnDate2);
//提交請求
result = mapPost("請求地址", obj2,"utf-8");
System.out.println(result);
}
}編碼