java HttpClient在Http協議接口測試中的使用

package http.client.test;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;




public class HttpClientTest {


public static void main(String[] args) {

String urlname = "qryEcChnlContent" ;//接口,我這裏寫死了,在顯示項目中,這個從客戶端獲取

String name = "亞信公告查詢";//調用接口中文名稱,現實中也是從客戶端傳過來

HttpClient hc= new HttpClient(); 

String url = hc.getUrl()+urlname;

//方法一:
//String json = "{\"bean\":{\"statMonth\":\"201612\",\"chnlCode\":\"731\",\"chnlId\":\"18177\"}}";//json 拼接不含字符串變量時用

String json = "{\"loginName\":\""+LOGIN_NAME+"\",\"start\":\""+START+"\",\"end\":\""+END+"\",\"bean\":{\"contentType\":\"1\"}}";//json拼接含字符串變量時用
String result= hc.post(name,url,json);

System.out.println(result);

//擴充:

Map maps = (Map)JSON.parse(result);//json字符串轉map


//方法二:
/*Map<String,String> map = new HashMap<String,String>();
map.put("bean", "1001011");
map.put("statMonth","20170807");
map.put("areaCode", "");
map.put("chnlCode", "51313");
map.put("chnlId", "11");
map.put("rwdTypeIds", "10000");


HttpClient hc= new HttpClient(); 
hc.post1(url,map);*/


}

}


package http.client.util;


import java.io.IOException;


import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
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.HttpClients;
import org.apache.http.util.EntityUtils;
/**

*封裝一個公共類

*

/

public class HttpClient {
private String name;//調用外圍接口的中文名字
private String url = "http://111.8.10.103:8082/channelTest?isconvert=true&action=";//調用外圍接口路徑
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String post(String name,String url, String body){
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
   try {
    httpClient = HttpClients.createDefault();
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build(); 
    httpPost = new HttpPost(url);
    httpPost.setConfig(requestConfig);
    httpPost.setEntity(new StringEntity(body));
    CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
//System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
result = EntityUtils.toString(httpEntity,"utf-8");
System.out.println(result);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(httpPost!=null){
httpPost.releaseConnection();
}
if(httpClient!=null){
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
   return result;
}


public void post1(String url, Map<String, String> params){
   CloseableHttpClient httpClient = null;
   HttpPost httpPost = null;

String result = null;   
   try {
       httpClient = HttpClients.createDefault();
       RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
       httpPost = new HttpPost(url);
       httpPost.setConfig(requestConfig);
       List<NameValuePair> ps = new ArrayList<NameValuePair>();
       for (String pKey : params.keySet()) {
           ps.add(new BasicNameValuePair(pKey, params.get(pKey)));
       }
       httpPost.setEntity(new UrlEncodedFormEntity(ps));
       CloseableHttpResponse response = httpClient.execute(httpPost);
       HttpEntity httpEntity = response.getEntity();
        //System.out.println(EntityUtils.toString(httpEntity,"utf-8"));

result = EntityUtils.toString(httpEntity,"utf-8");

System.out.println(result);
   } catch (ClientProtocolException e) {
       e.printStackTrace();
   } catch (IOException e) {
       e.printStackTrace();
   }finally{
       try {
           if(httpPost!=null){
               httpPost.releaseConnection();
           }
           if(httpClient!=null){
               httpClient.close();
           }
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
}
}



發佈了9 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章