2015-01-20 43 views
15

我必須使用HttpClient類向具有JSON內容的REST API服務發送刪除命令,並且無法使其工作。如何使用HttpClient將DELETE和JSON發送到REST API

API調用:

DELETE /xxx/current 
{ 
"authentication_token": "" 
} 

,因爲我不能添加任何內容到下面的語句:

HttpResponseMessage response = client.DeleteAsync(requestUri).Result; 

我知道如何使RestSharp這項工作:

var request = new RestRequest { 
    Resource = "/xxx/current", 
    Method = Method.DELETE, 
    RequestFormat = DataFormat.Json 
}; 

var jsonPayload = JsonConvert.SerializeObject(cancelDto, Formatting.Indented); 

request.Parameters.Clear(); 
request.AddHeader("Content-type", "application/json"); 
request.AddHeader ("Accept", "application/json"); 
request.AddParameter ("application/json", jsonPayload, ParameterType.RequestBody); 

var response = await client.ExecuteTaskAsync (request); 

但我已經完成了沒有RestSharp。

+0

(http://stackoverflow.com/questions/12022965/adding-http-headers-to-httpclient-asp-net -web-api)同時查看MSDN文章[HttpRequestMessage](http://msdn.microsoft.com/en-us/library/system.net.http.httprequestmessage(v = vs.118).aspx)。 – 2015-01-20 20:50:06

回答

0

嘗試

編輯

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.your.url"); 

request.Method = "DELETE"; 

request.ContentType = "application/json"; 
request.Accept = "application/json"; 

using (var streamWriter = new StreamWriter(request.GetRequestStream())) 
{ 
    string json = "{\"key\":\"value\"}"; 

    streamWriter.Write(json); 
    streamWriter.Flush(); 
} 

using (var httpResponse = (HttpWebResponse)request.GetResponse()) 
{ 
    // do something with response 
} 

Here你可以找到非常類似的問題。

編輯
我不知道該交接請求機構DELETE要求是不錯的辦法。特別是當這只是爲了您的身份驗證目的。我寧願將authentication_token添加到標題。這是因爲在我的解決方案中,我不必解析整個請求體來檢查當前請求是否被正確認證。其他請求類型呢?你總是通過authentication_token在請求正文?

24

雖然回答這個問題可能會晚,但 我遇到過類似的問題,下面的代碼對我有用。

HttpRequestMessage request = new HttpRequestMessage 
{ 
    Content = new StringContent("[YOUR JSON GOES HERE]", Encoding.UTF8, "application/json"), 
    Method = HttpMethod.Delete, 
    RequestUri = new Uri("[YOUR URL GOES HERE]") 
}; 
await httpClient.SendAsync(request); 
1

從Farzan Hajian的答案仍然沒有爲我工作,我可以設置請求的內容,但它並沒有實際發送到服務器。

作爲替代方案,您可以使用X-HTTP-Method-Override標題查看。這告訴服務器你想要它處理請求,就好像你發送了一個不同於你實際發送的動詞的動詞。您將不得不確保服務器正確處理此標頭,但是如果是這樣,您可以只發布請求並在標頭中添加:X-HTTP-Method-Override:DELETE,它將等同於具有正文的DELETE請求。

14

您可以使用這些擴展方法:[可能的地方開始]

public static class HttpClientExtensions 
{ 
    public static Task<HttpResponseMessage> DeleteAsJsonAsync<T>(this HttpClient httpClient, string requestUri, T data) 
     => httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Delete, requestUri) { Content = Serialize(data) }); 

    public static Task<HttpResponseMessage> DeleteAsJsonAsync<T>(this HttpClient httpClient, string requestUri, T data, CancellationToken cancellationToken) 
     => httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Delete, requestUri) { Content = Serialize(data) }, cancellationToken); 

    public static Task<HttpResponseMessage> DeleteAsJsonAsync<T>(this HttpClient httpClient, Uri requestUri, T data) 
     => httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Delete, requestUri) { Content = Serialize(data) }); 

    public static Task<HttpResponseMessage> DeleteAsJsonAsync<T>(this HttpClient httpClient, Uri requestUri, T data, CancellationToken cancellationToken) 
     => httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Delete, requestUri) { Content = Serialize(data) }, cancellationToken); 

    private static HttpContent Serialize(object data) => new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); 
} 
相關問題