locked
Windows Store app - how to make HttpClient POST request RRS feed

  • Question

  • I want to make a POST request similar to this example:

    POST /accesstoken.srf HTTP/1.1
    Content-Type: application/x-www-form-urlencoded
    Host: https://login.live.com
    Content-Length: 211
     
    grant_type=client_credentials&client_id=ms-app%3a%2f%2fS-1-15-2-2972962901-2322836549-3722629029-1345238579-3987825745-2155616079-650196962&client_secret=Vex8L9WOFZuj95euaLrvSH7XyoDhLJc7&scope=notify.windows.com
    

    THIS IS THE CODE I AM TRYING:

    uri theUri = new System.Uri("https://login.live.com"); HttpRequestMessage re = new HttpRequestMessage(); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded"); client.DefaultRequestHeaders.Host = theUri.Host; var body = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com", MyCredentials, MyCredentials2); HttpContent cont = new HttpContent(); StringContent theContent = new StringContent(body); HttpResponseMessage aResponse = await client.PostAsync(new Uri("https://login.live.com/accesstoken.srf"),theContent);


    When i run this i get an error saying bad request. I believe i a m doing this wrong since i am new at it.

    What is the correct way to write this POST request?

    Monday, July 22, 2013 11:05 PM

Answers

  • Firstly, the correct code to send a HTTP POST request with some content is the following:

    HttpClient client = new HttpClient();
    //var body = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com", MyCredentials, MyCredentials2);
    var body = String.Format("grant_type=client_credentials&client_id=ms-app%3a%2f%2fS-1-15-2-2972962901-2322836549-3722629029-1345238579-3987825745-2155616079-650196962&client_secret=Vex8L9WOFZuj95euaLrvSH7XyoDhLJc7&scope=notify.windows.com");
    StringContent theContent = new StringContent(body, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");
    HttpResponseMessage aResponse = await client.PostAsync(new Uri("https://login.live.com/accesstoken.srf"), theContent);

    The only thing that I am doing differently is that I am hardcoding the MyCredentials and MyCredentials2 parameters with the string values you have provided.

     

    With this code, I see the following HTTP request and response:

     

    Request:

    =======

    POST https://login.live.com/accesstoken.srf HTTP/1.1
    Content-Type: application/x-www-form-urlencoded; charset=utf-8
    Host: login.live.com
    Content-Length: 211
    Expect: 100-continue
    Connection: Keep-Alive
    grant_type=client_credentials&client_id=ms-app%3a%2f%2fS-1-15-2-2972962901-2322836549-3722629029-1345238579-3987825745-2155616079-650196962&client_secret=Vex8L9WOFZuj95euaLrvSH7XyoDhLJc7&scope=notify.windows.com

    Response:
    =========
    HTTP/1.1 400 Bad Request
    Cache-Control: no-store
    Content-Length: 66
    Content-Type: application/json
    Server: Microsoft-IIS/7.5
    X-WLID-Error: 0x80045A78
    PPServer: PPV: 30 H: BAYIDSLGN2G032 V: 0
    Date: Tue, 23 Jul 2013 23:57:59 GMT
    Connection: close

    {"error":"invalid_client","error_description":"Invalid client id"}

    So, it looks like the bad request was due to the invalid client ID.

    To troubleshoot this issue yourself, use Fiddler (www.fiddlertool.com) to decrypt the HTTPS stuff for you and you should be able to check the request/response yourself. http://fiddler2.com/documentation/Configure-Fiddler/Tasks/DecryptHTTPS

     


    Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, http://aka.ms/t4vuvz

    • Marked as answer by kalel111 Wednesday, July 24, 2013 3:49 AM
    Wednesday, July 24, 2013 12:04 AM
    Moderator

All replies

  • ANYONE????
    Tuesday, July 23, 2013 11:54 PM
  • Firstly, the correct code to send a HTTP POST request with some content is the following:

    HttpClient client = new HttpClient();
    //var body = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com", MyCredentials, MyCredentials2);
    var body = String.Format("grant_type=client_credentials&client_id=ms-app%3a%2f%2fS-1-15-2-2972962901-2322836549-3722629029-1345238579-3987825745-2155616079-650196962&client_secret=Vex8L9WOFZuj95euaLrvSH7XyoDhLJc7&scope=notify.windows.com");
    StringContent theContent = new StringContent(body, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");
    HttpResponseMessage aResponse = await client.PostAsync(new Uri("https://login.live.com/accesstoken.srf"), theContent);

    The only thing that I am doing differently is that I am hardcoding the MyCredentials and MyCredentials2 parameters with the string values you have provided.

     

    With this code, I see the following HTTP request and response:

     

    Request:

    =======

    POST https://login.live.com/accesstoken.srf HTTP/1.1
    Content-Type: application/x-www-form-urlencoded; charset=utf-8
    Host: login.live.com
    Content-Length: 211
    Expect: 100-continue
    Connection: Keep-Alive
    grant_type=client_credentials&client_id=ms-app%3a%2f%2fS-1-15-2-2972962901-2322836549-3722629029-1345238579-3987825745-2155616079-650196962&client_secret=Vex8L9WOFZuj95euaLrvSH7XyoDhLJc7&scope=notify.windows.com

    Response:
    =========
    HTTP/1.1 400 Bad Request
    Cache-Control: no-store
    Content-Length: 66
    Content-Type: application/json
    Server: Microsoft-IIS/7.5
    X-WLID-Error: 0x80045A78
    PPServer: PPV: 30 H: BAYIDSLGN2G032 V: 0
    Date: Tue, 23 Jul 2013 23:57:59 GMT
    Connection: close

    {"error":"invalid_client","error_description":"Invalid client id"}

    So, it looks like the bad request was due to the invalid client ID.

    To troubleshoot this issue yourself, use Fiddler (www.fiddlertool.com) to decrypt the HTTPS stuff for you and you should be able to check the request/response yourself. http://fiddler2.com/documentation/Configure-Fiddler/Tasks/DecryptHTTPS

     


    Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, http://aka.ms/t4vuvz

    • Marked as answer by kalel111 Wednesday, July 24, 2013 3:49 AM
    Wednesday, July 24, 2013 12:04 AM
    Moderator