Monday, July 29, 2013

Windows Store app - how to make HttpClient POST request

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?


No comments:

Post a Comment