quocvuongdn

#java: send POST with specified data fields

Sep 22nd, 2014
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. public String sendPost(Map<String, NameValuePair> paramList, String paramNames){
  2.         String json_result = "";
  3.  
  4.         List<NameValuePair> input = new ArrayList<>();
  5.         String[] split = paramNames.split(",");
  6.         for (String str : split) {
  7.             if (paramList.get(str.trim()) != null) {
  8.                 input.add(paramList.get(str.trim()));
  9.             }
  10.         }
  11.        
  12.         String query = URLEncodedUtils.format(input, "utf-8");
  13.        
  14.         //Create post http client
  15.         CloseableHttpClient httpClient = HttpClients.createDefault();
  16.         HttpGet httpGet = new HttpGet(ME_URL+query);
  17.         try {
  18.             try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
  19.                 HttpEntity resEntity = response.getEntity();
  20.  
  21.                 if (resEntity != null) {
  22.                     json_result = EntityUtils.toString(resEntity);
  23.                 } else{
  24.                     json_result = ME_URL + query;
  25.                 }
  26.             }
  27.         } catch (IOException ex) {
  28.             Logger.getLogger(ProcessRequest.class.getName()).log(Level.SEVERE, null, ex);
  29.         }
  30.         return json_result;
  31.     }
  32.  
  33. // Using HttpClient 4.3 lib
  34. //Input:
  35. //paramList: MAP<String KEY, BasicNameValuePair INPUT_PARAM>
  36. //paramNames: String of specified fields, ex: name, age, value.
  37. //This func plays a mediating role in the connection between a client and a server.
  38.  
  39. //Client A send somedata (ex: name, age, address, job) << sendPOST filter data fields and CURL to >> Server B receive only specified data fields (ex: name, age, address)
Advertisement
Add Comment
Please, Sign In to add comment