Java http 請求三種形式
阿新 • • 發佈:2018-12-27
JavaHttp介面互動的兩種方式
JavaHttp介面開發針對表單提交跟Json提交的兩種方式
表單提交(post、get)
get表單引數提交
/** * @param type * @param url * @param map * @return * GY * 2018年1月2日 * url:請求地址 * map:請求引數 * Get有參請求 * @throws IOException * @throws ClientProtocolException
*/
public static String
HttpSendGet(String url, Map<String,String> map)
throws ClientProtocolException,
IOException{
String resultStr = null;
if(map==null){
return resultStr;
}
CloseableHttpClient httpclient =
HttpClients.createDefault();
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String,
String> entry : map.entrySet())
{
//給引數賦值
//gson.toJson(entry.getValue())
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
String paramStr =
EntityUtils.toString(new UrlEncodedFormEntity( formparams,
Consts.UTF_8));
HttpGet httpget = new HttpGet(url+"?"+paramStr);
CloseableHttpResponse response = httpclient.execute(httpget);
HttpEntity responseEntity = response.getEntity();
if(responseEntity != null){
resultStr =
EntityUtils.toString(responseEntity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return resultStr;
}
post表單提交
/**
* @param url
* @param map
* @return
* GY
* 2018年1月2日
* url:請求地址
* map:請求引數
* Post有參請求
* @throws IOException
* @throws ClientProtocolException
*/
public static String
HttpSendPost(String url, Map<String,String> map)
throws ClientProtocolException,
IOException{
if(map==null){
return null;
}
String resultStr = null;
//1.獲得一個httpclient物件
CloseableHttpClient httpclient =
HttpClients.createDefault();
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String,
String> entry : map.entrySet())
{
//給引數賦值
//gson.toJson(entry.getValue())
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
Consts.UTF_8);
//System.out.println(EntityUtils.toString(entity));
//2.生成一個post請求
HttpPost httppost = new HttpPost(url);
httppost.setEntity(entity);
//3.執行post請求並返回結果
CloseableHttpResponse response = httpclient.execute(httppost);
//4.處理結果,這裡將結果返回為字串
HttpEntity responseEntity = response.getEntity();
if(responseEntity != null){
resultStr =
EntityUtils.toString(responseEntity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return resultStr;
}
get無參提交
/** * 傳送HttpGet請求 * @param url * @return * @throws IOException * @throws ClientProtocolException * GY * 2018年1月2日 * Get無參請求 */ public static String sendGet(String url) throws ClientProtocolException, IOException { //1.獲得一個httpclient物件 CloseableHttpClient httpclient = HttpClients.createDefault(); //2.生成一個get請求 HttpGet httpget = new HttpGet(url); CloseableHttpResponse response = null; //3.執行get請求並返回結果 response = httpclient.execute(httpget); String result = null; //4.處理結果,這裡將結果返回為字串 HttpEntity entity = response.getEntity(); if (entity != null) { result = EntityUtils.toString(entity); } if(httpclient!=null){ httpclient.close(); } if(response!=null){ response.close(); } return result; } Json資料型別提交
/** * @param url * @param json * @return * @throws ClientProtocolException * @throws IOException * GY * 2018年1月4日 * 傳送json 資料 http client */ public static String HttpSendJsonPost(String url, String json) throws ClientProtocolException, IOException{ if(StringUtils.isEmpty(json)){ return null; } String resultStr = null; //1.獲得一個httpclient物件 CloseableHttpClient httpclient = HttpClients.createDefault(); StringEntity postingString = new StringEntity(json, Consts.UTF_8);// json傳遞 postingString.setContentEncoding("UTF-8"); postingString.setContentType("application/json"); System.out.println(EntityUtils.toString(postingString)); //2.生成一個post請求 HttpPost httpPost = new HttpPost(url); httpPost.setEntity(postingString); //3.執行post請求並返回結果 CloseableHttpResponse response = httpclient.execute(httpPost); //4.處理結果,這裡將結果返回為字串 HttpEntity responseEntity = response.getEntity(); if(responseEntity != null){ resultStr = EntityUtils.toString(responseEntity); } if(httpclient!=null){ httpclient.close(); } if(response!=null){ response.close(); } return resultStr; }
/** * @param type * @param url * @param map * @return * GY * 2018年1月2日 * url:請求地址 * map:請求引數 * Get有參請求 * @throws IOException * @throws ClientProtocolException
/** * 傳送HttpGet請求 * @param url * @return * @throws IOException * @throws ClientProtocolException * GY * 2018年1月2日 * Get無參請求 */ public static String sendGet(String url) throws ClientProtocolException, IOException { //1.獲得一個httpclient物件 CloseableHttpClient httpclient = HttpClients.createDefault(); //2.生成一個get請求 HttpGet httpget = new HttpGet(url); CloseableHttpResponse response = null; //3.執行get請求並返回結果 response = httpclient.execute(httpget); String result = null; //4.處理結果,這裡將結果返回為字串 HttpEntity entity = response.getEntity(); if (entity != null) { result = EntityUtils.toString(entity); } if(httpclient!=null){ httpclient.close(); } if(response!=null){ response.close(); } return result; } Json資料型別提交
/** * @param url * @param json * @return * @throws ClientProtocolException * @throws IOException * GY * 2018年1月4日 * 傳送json 資料 http client */ public static String HttpSendJsonPost(String url, String json) throws ClientProtocolException, IOException{ if(StringUtils.isEmpty(json)){ return null; } String resultStr = null; //1.獲得一個httpclient物件 CloseableHttpClient httpclient = HttpClients.createDefault(); StringEntity postingString = new StringEntity(json, Consts.UTF_8);// json傳遞 postingString.setContentEncoding("UTF-8"); postingString.setContentType("application/json"); System.out.println(EntityUtils.toString(postingString)); //2.生成一個post請求 HttpPost httpPost = new HttpPost(url); httpPost.setEntity(postingString); //3.執行post請求並返回結果 CloseableHttpResponse response = httpclient.execute(httpPost); //4.處理結果,這裡將結果返回為字串 HttpEntity responseEntity = response.getEntity(); if(responseEntity != null){ resultStr = EntityUtils.toString(responseEntity); } if(httpclient!=null){ httpclient.close(); } if(response!=null){ response.close(); } return resultStr; }