1

I'm trying pass a string in the body of my POST request using WebClient class. Below is my code:

protected void GetAccessToken(string client_id, string secret)
    {
        var urlEncodedSecret = HttpUtility.UrlEncode(secret);
        var urlEncodedSid = HttpUtility.UrlEncode(client_id);

        string bodyData = String.Format("grant_type=client_credentials&client_id=ms-app%3a%2f%2f{0}&client_secret={1}&scope=notify.windows.com", client_id, secret);

        string address = "https://login.live.com/accesstoken.srf";
        string myParameters = "param1=value1&param2=value2&param3=value3";

        Uri URI = new Uri(address);
        System.Net.WebClient webClient = new WebClient();
        webClient.BaseAddress = address;

        webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
        webClient.Headers["Content-Length"] = "211";
        webClient.Headers["Host"] = "https://login.live.com";

        webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(uploadStringCompleted);
        webClient.UploadStringAsync(URI, "POST", bodyData);
    }

    private void uploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
        string json = e.Result.ToString();
        _oAuthToken = GetOAuthTokenFromJson(json);
    }

When i run the application, i received the exception "The remote server returned an error: NotFound.". I succeeded to make the same request using the Postman: Postman requisition

Anyone know something wrong about my request? Or something that puting me on a right way!?

alvaropaco
  • 1,573
  • 18
  • 29
  • 1
    Have you ever used Fiddler? I would recommend using that to catch the request to see exactly what is being passed. – tc44 Oct 28 '15 at 20:59
  • tc44 I never have used Fiddler, but i will search about this. Thank you. – alvaropaco Oct 28 '15 at 21:03
  • Are you sure, that your content length is *211*? This is normally in bytes - the bytes of the posted data. – keenthinker Oct 28 '15 at 21:16
  • Yes, i'am sure. By documentation of the web service api, i need pass Content-Lenght equals to "211". – alvaropaco Oct 28 '15 at 21:18
  • 1
    If I were you I would look at http://stackoverflow.com/questions/20064505/requesting-html-over-https-with-c-sharp-webclient http://stackoverflow.com/questions/536352/webclient-https-issues but my guess is http://stackoverflow.com/questions/11328061/c-sharp-webclient-download-string-https – Peuczynski Oct 28 '15 at 22:47

0 Answers0