Httpclient httpdelete 參數

Httpclient 中經常使用的請求有2個,HttpPost 和 HttpGet,今天在對某個網站進行分析的時候,忽然發現用到了 HttpDelete,而且傳參 是 Json。java

一、通常 HttpPost 對傳參 Json 的處理是:apache

 

 // 中文處理json

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

httppost.setEntity(se);cookie

 

 

二、使用 HttpDelete,貌似不能傳參,突發奇想,將 HttpDelete 換成 HttpPost,再傳參,此路不通app

 

 

三、百度沒有找到很好的解決方法。只好 Google, HttpDelete Json,在 stackoverflow 上看了幾篇文章,立馬找到解決辦法了 post

 詳見 http://stackoverflow.com/questions/3773338/httpdelete-with-body網站

 

四、解決辦法:spa

 

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import java.net.URI;
import org.apache.http.annotation.NotThreadSafe;

@NotThreadSafe
class 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(); }
}

 而後就簡單了.net

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,大夥自個百度。

相關文章
相關標籤/搜索