package com.util; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.HttpClientUtils; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; /* * Tool class for post request using HttpClient */ @SuppressWarnings("deprecation") public class ApacheHttpClientUtil { /** * HttpClient post request method * @param url request address * @param data push string * @return String return value */ public static String HttpPostSting(String url, String data) { String returnValue = "This is the default return value, the interface call failed"; HttpClient httpClient = null; ResponseHandler<String> responseHandler = new BasicResponseHandler(); try { // Step 1: Create the HttpClient object httpClient = HttpClients.createDefault(); // Step 2: Create the httpPost object HttpPost httpPost = new HttpPost(url); // Step 3: Set parameters for httpPost StringEntity requestEntity = new StringEntity(data, "utf-8"); requestEntity.setContentEncoding("UTF-8"); httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); httpPost.setEntity(requestEntity); // Step 4: Send HttpPost request and get the return value returnValue = httpClient.execute(httpPost, responseHandler); // This method must be used when calling the interface to get the return value // CloseableHttpResponse httpResonse = httpClient.execute(httpPost); // int statusCode = httpResonse.getStatusLine().getStatusCode(); // if(statusCode!=200) // { // System.out.println("The request failed to send, the return parameter of the failure is: "+httpResonse.getStatusLine()); // returnValue = httpResonse.getStatusLine().toString(); // } // } catch (Exception e) { e.printStackTrace (); } finally { HttpClientUtils.closeQuietly(httpClient); } // Step 5: Process the return value return returnValue; } /** * HttpClient post request method * @param url request address * @param map push parameters * @param charset push parameter encoding format * @return String return value */ public static String HttpPostMap(String url,Map<String,String> map,String charset){ HttpClient httpClient = null; HttpPost httpPost = null; String result = null; try{ httpClient = HttpClients.createDefault(); httpPost = new HttpPost(url); //Setting parameters List<NameValuePair> list = new ArrayList<NameValuePair>(); Iterator iterator = map.entrySet().iterator(); while(iterator.hasNext()){ Entry<String,String> elem = (Entry<String, String>) iterator.next(); list.add(new BasicNameValuePair(elem.getKey(),elem.getValue())); } if(list.size() > 0){ UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); if(response != null){ HttpEntity resEntity = response.getEntity(); if(resEntity != null){ result = EntityUtils.toString(resEntity,charset); } } } }catch(Exception ex){ ex.printStackTrace(); } return result; } }
apache's http transport
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326118505&siteId=291194637
Recommended
Ranking