httpputdelete方式请求

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

httpputdelete⽅式请求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类 1import .URI;
2
3import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
4
5public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase{
6public static final String METHOD_NAME = "DELETE";
7
8/**
9 * 获取⽅法(必须重载)
10 *
11 * @return
12*/
13 @Override
14public String getMethod() {
15return METHOD_NAME;
16 }
17
18public HttpDeleteWithBody(final String uri) {
19super();
20 setURI(URI.create(uri));
21 }
22
23public HttpDeleteWithBody(final URI uri) {
24super();
25 setURI(uri);
26 }
27
28public HttpDeleteWithBody() {
29super();
30 }
31
32 }
3. ⽤HttpClient 调⽤ HttpDeleteWithBody的⽅法,就可以进⾏body的操作了
1public static String doDelete(String data, String url) throws IOException {
2 CloseableHttpClient client = null;
3 HttpDeleteWithBody httpDelete = null;
4 String result = null;
5try {
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
17if (200 == response.getStatusLine().getStatusCode()) {
18 ("DELETE⽅式请求远程调⽤成功.msg={}", result);
19 }
20 } catch (Exception e) {
21 logger.error("DELETE⽅式请求远程调⽤失败,errorMsg={}", e.getMessage());
22 } finally {
23 client.close();
24 }
25return result;
26
27 }
28 }
HttpClient使⽤put⽅式提交数据
1public static String httpPut(String urlPath, String data, String charSet, String[] header)
2 {
3 String result = null;
4 URL url = null;
5 HttpURLConnection httpurlconnection = null;
6try
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
15if (header != null)
16 {
17for (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
28if (StringUtils.isNotBlank(data))
29 {
30 httpurlconnection.getOutputStream().write(data.getBytes("UTF-8"));
31 }
32 httpurlconnection.getOutputStream().flush();
33 httpurlconnection.getOutputStream().close();
34int code = httpurlconnection.getResponseCode();
35
36if (code == 200)
37 {
38 DataInputStream in = new DataInputStream(httpurlconnection.getInputStream());
39int len = in.available();
40byte[] by = new byte[len];
41 in.readFully(by);
42if (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;
60if (httpurlconnection != null)
61 {
62 httpurlconnection.disconnect();
63 }
64 }
65return result;
66 }。

相关文档
最新文档