如何通过C#WebClient发送HTTP DELETE请求?

时间:2015-05-11 07:59:52

标签: c# android .net rest xamarin

登录后,我可以从远程系统获得令牌。例如,authentication_token是8JySqFVx_pKx_3nx67AJ 我可以通过这个命令从终端注销

        WebClient wc = new WebClient();
        string baseSiteString = wc.DownloadString("https://sample.com");
        string csrfToken = Regex.Match(baseSiteString, "<meta name=\"csrf-token\" content=\"(.*?)\" />").Groups[1].Value;
        string cookie = wc.ResponseHeaders[HttpResponseHeader.SetCookie];

        wc.Headers.Add(HttpRequestHeader.Cookie, cookie);
        wc.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8");
        wc.Headers.Add(HttpRequestHeader.Accept, "application/json, text/javascript, */*; q=0.01");
        wc.Headers.Add("X-CSRF-Token", csrfToken);
        wc.Headers.Add("X-Requested-With", "XMLHttpRequest");


        string dataString  =  @"{""user"":{""email"":""sample@sample.com"",""password"":""sample_password""}}";
        byte[] dataBytes = Encoding.UTF8.GetBytes(dataString);
        byte[] responseBytes = wc.UploadData(new Uri("https://sample.com/auth.json"), "POST", dataBytes);
        string responseString = Encoding.UTF8.GetString(responseBytes);

我需要从C#中做到这一点。 我可以像这样发送POST请求:

{{1}}

如何从C#发送DELETE请求,其中authentication_token是参数?

2 个答案:

答案 0 :(得分:4)

解决了,我使用了WebClient。

        var wc2 = new WebClient();
        string baseSiteString2 = wc2.DownloadString("https://sample.com");
        string csrfToken2 = Regex.Match(baseSiteString2, "<meta name=\"csrf-token\" content=\"(.*?)\" />").Groups[1].Value;
        string cookie2 = wc2.ResponseHeaders[HttpResponseHeader.SetCookie];

        wc2.Headers.Add(HttpRequestHeader.Cookie, cookie2);
        wc2.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8");
        wc2.Headers.Add(HttpRequestHeader.Accept, "application/json, text/javascript, */*; q=0.01");
        wc2.Headers.Add("X-CSRF-Token", csrfToken2);
        wc2.Headers.Add("X-Requested-With", "XMLHttpRequest");


        string dataString2 = "";// @"{""name"":""Megan"",""regi_number"":4444}";
        byte[] dataBytes2 = Encoding.UTF8.GetBytes(dataString2);
        string finalUrl = "https://sample.com?authentication_token=" + authentication_token;
        byte[] responseBytes2 = wc2.UploadData(new Uri(finalUrl), "DELETE", dataBytes2);
        string responseString2 = Encoding.UTF8.GetString(responseBytes2);
        authentication_token = "";         

答案 1 :(得分:2)

为什么不使用更简单的HttpClient来完成这项工作? (您通过http发布的现有代码可以通过使用HttpClient进行大幅改进/简化)

你可以这样做:

private HttpClient GetHttpClient()
{
    // Initialize client with default headers, base address, etc.
    var httpClient = new HttpClient();
    httpClient.BaseAddress = new Uri("https://sample.com");
    httpClient.DefaultRequestHeaders.Accept.Clear();
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    // Set some other headers if necessary...

    return httpClient;
}

private async Task DeleteUser(string token)
{
    using (var httpClient = GetHttpClient())
        await httpClient.DeleteAsync("api/users/delete?token=" + token);
}

上面的内容是异步的(因为一切都在HttpClient类中。但是你可以像这样创建一个同步方法:

private void DeleteUser(string token)
{
    using (var httpClient = GetHttpClient())
        httpClient.DeleteAsync("api/users/delete?token=" + token).Result;
}