WebClient on WP7 - Throw "A request with this method cannot have a request body"

Posted by Peter Hansen on Stack Overflow See other posts from Stack Overflow or by Peter Hansen
Published on 2012-03-22T14:39:33Z Indexed on 2012/03/22 17:29 UTC
Read the original article Hit count: 1375

If I execute this code in a Consoleapp it works fine:

        string uriString = "http://url.com/api/v1.0/d/" + Username + "/some?amount=3&offset=0";

        WebClient wc = new WebClient();

        wc.Headers["Content-Type"] = "application/json";
        wc.Headers["Authorization"] = AuthString.Replace("\\", "");

        string responseArrayKvitteringer = wc.DownloadString(uriString);
        Console.WriteLine(responseArrayKvitteringer);

But if I move the code to my WP7 project like this:

        string uriString = "http://url.com/api/v1.0/d/" + Username + "/some?amount=3&offset=0";

            WebClient wc = new WebClient();

            wc.Headers["Content-Type"] = "application/json";
            wc.Headers["Authorization"] = AuthString.Replace("\\", "");

            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            wc.DownloadStringAsync(new Uri(uriString));

    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        MessageBox.Show(e.Result);
    }

I got the exception: A request with this method cannot have a request body.

Why?

The solution is to remove the Content-type:

   string uriString = "http://url.com/api/v1.0/d/" + Username + "/some?amount=3&offset=0";

            WebClient wc = new WebClient();

            //wc.Headers["Content-Type"] = "application/json";
            wc.Headers["Authorization"] = AuthString.Replace("\\", "");

            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            wc.DownloadStringAsync(new Uri(uriString));

    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        MessageBox.Show(e.Result);
    }

© Stack Overflow or respective owner

Related posts about windows-phone-7

Related posts about webclient