博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Httpclient httpdelete 参数
阅读量:4507 次
发布时间:2019-06-08

本文共 1378 字,大约阅读时间需要 4 分钟。

Httpclient 中常用的请求有2个,HttpPost 和 HttpGet,今天在对某个网站进行分析的时候,突然发现用到了 HttpDelete,并且传参 是 Json。

1、一般 HttpPost 对传参 Json 的处理是:

 

 // 中文处理

StringEntity se = new StringEntity(json, Consts.UTF_8);

httppost.setEntity(se);

 

 

2、使用 HttpDelete,貌似不能传参,突发奇想,将 HttpDelete 换成 HttpPost,再传参,此路不通。

 

 

3、百度没有找到很好的解决方法。只好 Google, HttpDelete Json,在 stackoverflow 上看了几篇文章,立马找到解决办法了 

 详见 

 

4、解决办法:

 

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;import java.net.URI;import org.apache.http.annotation.NotThreadSafe;@NotThreadSafeclass HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {    public static final String METHOD_NAME = "DELETE";    public String getMethod() { return METHOD_NAME; }    public HttpDeleteWithBody(final String uri) {        super();        setURI(URI.create(uri));    }    public HttpDeleteWithBody(final URI uri) {        super();        setURI(uri);    }    public HttpDeleteWithBody() { super(); }}

 然后就简单了

httpdelete.setHeader("Cookie", cookie); // json 处理httpdelete.setHeader("Content-Type", "application/json; charset=UTF-8");httpdelete.setHeader("X-Requested-With", "XMLHttpRequest");            httpdelete.setEntity(new StringEntity(json));httpdelete.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000);HttpResponse response = client.execute(httpdelete);

 

最近 Google 大神很不方便,推荐一款浏览器,Buckyball,大伙自个百度。

 

转载于:https://www.cnblogs.com/heyus/p/3790234.html

你可能感兴趣的文章
错误“该伙伴事务管理器已经禁止了它对远程/网络事务的支持”解决方案
查看>>
System x 服务器制作ServerGuide U盘安装Windows Server 2008 操作系统 --不格式化盘
查看>>
java面试
查看>>
前端常见跨域解决方案(全)
查看>>
单点登录(Single Sign On)解决方案
查看>>
umi---className设置多个样式
查看>>
网页包抓取工具Fiddler工具简单设置
查看>>
周总结报告
查看>>
Selecting Courses POJ - 2239(我是沙雕吧 按时间点建边 || 匹配水题)
查看>>
Win+R指令(2)
查看>>
codeforces 578c - weekness and poorness - 三分
查看>>
数值微分方程
查看>>
动态规划--电路布线(circuit layout)
查看>>
<转>OD常用断点列表
查看>>
描边时消除锯齿SetSmoothingMode
查看>>
15回文相关问题
查看>>
将VS2013项目转成VS2010项目的方法
查看>>
[置顶] 怎么对待重复的代码
查看>>
多种方法实现H5网页图片动画效果;
查看>>
Ubuntu/CentOS下使用脚本自动安装 Docker
查看>>