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 }