1

After logging in I can have a token from my remote system. For example the authentication_token is 8JySqFVx_pKx_3nx67AJ I can log out from terminal via this command

curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X DELETE https://sample.com\?authentication_token\=8JySqFVx_pKx_3nx67AJ

I need to do it from C#. I can send POST request like this:

        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"":""[email protected]"",""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);

How Can I send DELETE request from C# where authentication_token would be a parameter?

2
  • 1
    you can also use HttpClient class, see asp.net/web-api/overview/advanced/… for samples Commented May 11, 2015 at 8:11
  • I solved it. Check answer section. Commented May 12, 2015 at 8:35

3 Answers 3

7

Solved, I used 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 = "";         
Sign up to request clarification or add additional context in comments.

1 Comment

Good one.. only I am not sure @Reza why do we have to say New URI in uploaddata?
4

Why not use the much simpler HttpClient for this job? (Your existing code to post via http could be heavely improved/simplyfied by using the HttpClient)

You could do something like this:

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);
}

The above is async though, (as everything is in the HttpClient class. You could however create a synchronous method like this:

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

Comments

0

You don't really need to use a WebClient for sending a DELETE request. I use a simple HttpWebRequest like so:

var url = "https://yourwebservice";

var httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "DELETE";
httpRequest.Accept = "*/*";
httpRequest.Headers["Authorization"] = "yourauth";

var httpResponse = (HttpWebResponse)httpRequest.GetResponse();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.