HttpClient使用Delete方式提交數據
1. http請求主要有以下幾種方法來對指定資源做不同操作:
1 HTTP/1.1協議中共定義了八種方法(有時也叫“動作”)來表明Request-URI指定的資源的不同操作方式: 2 . OPTIONS - 返回服務器針對特定資源所支持的HTTP請求方法。 3 也可以利用向Web服務器發送'*'的請求來測試服務器的功能性。 4 . HEAD - 向服務器索要與GET請求相一致的響應,只不過響應體將不會被返回。 5 這一方法可以在不必傳輸整個響應內容的情況下,就可以獲取包含在響應消息頭中的元信息。 6 . GET - 向特定的資源發出請求。 7 注意:GET方法不應當被用於產生“副作用”的操作中,例如在web app.中。 8 其中一個原因是GET可能會被網絡蜘蛛等隨意訪問。 9 . POST - 向指定資源提交數據進行處理請求(例如提交表單或者上傳文件)。 10 數據被包含在請求體中。POST請求可能會導致新的資源的建立和/或已有資源的修改。 11 . PUT - 向指定資源位置上傳其最新內容。 12 . DELETE - 請求服務器刪除Request-URI所標識的資源。 13 . TRACE - 回顯服務器收到的請求,主要用於測試或診斷。 14 . CONNECT - HTTP/1.1協議中預留給能夠將連接改為管道方式的代理服務器。
2.HttpDelete的方法中本身並沒有setEntity方法,參考HttpPost的setEntity方法,自定義一個HttpDeleteWithBody類
1 import java.net.URI; 2 3 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; 4 5 public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase{ 6 public static final String METHOD_NAME = "DELETE"; 7 8 /** 9 * 獲取方法(必須重載) 10 * 11 * @return 12 */ 13 @Override 14 public String getMethod() { 15 return METHOD_NAME; 16 } 17 18 public HttpDeleteWithBody(final String uri) { 19 super(); 20 setURI(URI.create(uri)); 21 } 22 23 public HttpDeleteWithBody(final URI uri) { 24 super(); 25 setURI(uri); 26 } 27 28 public HttpDeleteWithBody() { 29 super(); 30 } 31 32 }
3. 用HttpClient 調用 HttpDeleteWithBody的方法,就可以進行body的操作了
1 public static String doDelete(String data, String url) throws IOException { 2 CloseableHttpClient client = null; 3 HttpDeleteWithBody httpDelete = null; 4 String result = null; 5 try { 6 client = HttpClients.createDefault(); 7 httpDelete = new HttpDeleteWithBody(url); 8 9 httpDelete.addHeader("Content-type","application/json; charset=utf-8"); 10 httpDelete.setHeader("Accept", "application/json; charset=utf-8"); 11 httpDelete.setEntity(new StringEntity(data)); 12 13 CloseableHttpResponse response = client.execute(httpDelete); 14 HttpEntity entity = response.getEntity(); 15 result = EntityUtils.toString(entity); 16 17 if (200 == response.getStatusLine().getStatusCode()) { 18 logger.info("DELETE方式請求遠程調用成功.msg={}", result); 19 } 20 } catch (Exception e) { 21 logger.error("DELETE方式請求遠程調用失敗,errorMsg={}", e.getMessage()); 22 } finally { 23 client.close(); 24 } 25 return result; 26 27 } 28 }
HttpClient使用put方式提交數據
1 public static String httpPut(String urlPath, String data, String charSet, String[] header) 2 { 3 String result = null; 4 URL url = null; 5 HttpURLConnection httpurlconnection = null; 6 try 7 { 8 url = new URL(urlPath); 9 httpurlconnection = (HttpURLConnection) url.openConnection(); 10 httpurlconnection.setDoInput(true); 11 httpurlconnection.setDoOutput(true); 12 httpurlconnection.setConnectTimeout(2000000);// 設置連接主機超時(單位:毫秒) 13 httpurlconnection.setReadTimeout(2000000);// 設置從主機讀取數據超時(單位:毫秒) 14 15 if (header != null) 16 { 17 for (int i = 0; i < header.length; i++) 18 { 19 String[] content = header[i].split(":"); 20 httpurlconnection.setRequestProperty(content[0], content[1]); 21 } 22 } 23 24 httpurlconnection.setRequestMethod("PUT"); 25 httpurlconnection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); 26 // httpurlconnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 27 28 if (StringUtils.isNotBlank(data)) 29 { 30 httpurlconnection.getOutputStream().write(data.getBytes("UTF-8")); 31 } 32 httpurlconnection.getOutputStream().flush(); 33 httpurlconnection.getOutputStream().close(); 34 int code = httpurlconnection.getResponseCode(); 35 36 if (code == 200) 37 { 38 DataInputStream in = new DataInputStream(httpurlconnection.getInputStream()); 39 int len = in.available(); 40 byte[] by = new byte[len]; 41 in.readFully(by); 42 if (StringUtils.isNotBlank(charSet)) 43 { 44 result = new String(by, Charset.forName(charSet)); 45 } else 46 { 47 result = new String(by); 48 } 49 in.close(); 50 } else 51 { 52 logger.error("請求地址:" + urlPath + "返回狀態異常,異常號為:" + code); 53 } 54 } catch (Exception e) 55 { 56 logger.error("訪問url地址:" + urlPath + "發生異常", e); 57 } finally 58 { 59 url = null; 60 if (httpurlconnection != null) 61 { 62 httpurlconnection.disconnect(); 63 } 64 } 65 return result; 66 }