1,HttpPut
CloseableHttpClient httpclient = HttpClientFactory
.createBasicHtppClient();
HttpPut put = new HttpPut(url+"?"+concatParams(map));
StringEntity params =new StringEntity(JSONObject.fromObject(map).toString(),"utf-8");
put.setHeader("Content-type", "application/json");
put.setEntity(params);
HttpResponse response = httpclient.execute(put);
System.out.println("Response Code:"+response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println("result:"+result);
2,HttpPost
CloseableHttpClient httpclient = HttpClientFactory
.createBasicHtppClient();
StringPostRequest stringPostRequest=new StringPostRequest(url);
stringPostRequest.setQueryMap(map);
stringPostRequest.setContentType("application/json");
stringPostRequest.setContent(JSONObject.fromObject(map).toString());
String request=stringPostRequest.execute(httpclient);
System.out.println(request);
3,HttpDelete
CloseableHttpClient httpclient = HttpClientFactory.createBasicHtppClient();
HttpDelete httpDelete = new HttpDelete(url+"?"+concatParams(map));
HttpResponse response = httpclient.execute(httpDelete);
System.out.println("Response Code:"+response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println("result:"+result);
4,HttpGet
CloseableHttpClient httpclient = HttpClientFactory.createBasicHtppClient();
HttpGetRequest httpGet = new HttpGetRequest(url);
httpGet.setQueryMap(map);
String repose = httpGet.execute(httpclient);
System.out.println(repose);
参数用&链接并按字母排序
public static String concatParams(Map<String, String> params2) {
Object[] key_arr = params2.keySet().toArray();
Arrays.sort(key_arr);
String str = "";
for (Object key : key_arr) {
String val = params2.get(key);
str += "&" + key + "=" + val;
}
return str.replaceFirst("&", "");
}