0

In the console, I follow up a call the site I'm on is is making and I can see the address (some.site.com/gettoken), message header and something that FF calls Message Body. It's in the latter that I can see the credentials that I've entered on the site that are being sent.

So, I've got the URL and the message body. Then, I've tried to implement the behavior using C# for my Azure service layer like so.

String url = @"https://some.site.com/gettoken";
String credentials = "username=super&password=secret";
using (WebClient client = new WebClient())
{
  String output = client.UploadString(url, credentials);
  result = output;
}

However, I get error 400 - bad result. What did I miss?

I've googled for some stuff but the only remotely relevant hits are talking about the upload methods, which I've used. Am I barking up the wrong tree entirely or just missing something tiny? Some people seem to get it to work but they're not tokenizing around. And I'm not certain enough to determine whether it's of relevance or not.

Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • Can you use [`HttpClient`](http://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.118).aspx)? Your code would be `client.PostAsync(url, new HttpStringContent(credentials));` then. Or `new FormUrlEncodedContent` if you don't manually build the string. I'm not entirely certain how the `WebClient` does this. – Jeroen Vannevel Nov 12 '14 at 18:08
  • I've been suggested to use *WebClient* because it's smoother than *HttpWebRequest* but a [internet](http://stackoverflow.com/a/23949674/1525840) says that *HttpClient* is even newer/better, so sure, why not. However, the asynchronous part is worrying me a bit. I need to obtain the token **before** doing anything else. Sync seems to be the safest way, doesn't it? – Konrad Viltersten Nov 12 '14 at 18:17
  • If you just `await` the call you will be fine; execution of the method won't continue until the call is finished. – Jeroen Vannevel Nov 12 '14 at 18:19
  • Hmm... Seems that I can't find *Http**String**Content*. Only *HttpContent* and it's got an empty constructor... I did reference the ass for *HttpClient*, of course. What more could that be? Did you typo'ed? *Http.StringContent*? Also, I can only find *Wait* no *Await* in the *Task* object returned. Is that it? – Konrad Viltersten Nov 12 '14 at 18:28
  • it's `System.net.http.StringContent`. As to the wait/await: that's part of the *TPL*, more on that here: http://msdn.microsoft.com/en-us/library/hh191443.aspx – Jeroen Vannevel Nov 12 '14 at 18:29
  • OK, two things. (a) Post this as a reply because it seems to be the most concurrent usage of the libraries and seems to be nicely compact and pro. (b) The software doesn't crash and burn anymore so I'm guessing you got me going - but where is the token?! I can't find anything in the *Task* object... – Konrad Viltersten Nov 12 '14 at 18:32
  • Darn... I'm not quite there yet. I get this when I *ToString* the *Result* property: StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:\u000d\u000a{\u000d\u000a Transfer-Encoding: chunked\u000d\u000a Connection: keep-alive\u000d\u000a Vary: Accept-Encoding\u000d\u000a X-Frame-Options: SAMEORIGIN\u000d\u000a Date: Wed, 12 Nov 2014 18:34:28 GMT\u000d\u000a Set-Cookie: lang=\"en\";Max-Age=31622400;expires=Fri, 13-Nov-2015 18:34:27 GMT;Path=\/;Version=\"1\" Server: nginx\/1.4.1 Server: (Ubuntu) Content-Type: text\/html; charset=utf-8} – Konrad Viltersten Nov 12 '14 at 18:37

1 Answers1

1

So, as a summary of what has been discussed in the comments: you can use the more modern HttpClient instead.

Note that this is the System.Net.Http.HttpClient and not Windows.Web.Http.HttpClient.

An example implementation could look like this:

public async Task<string> SendCredentials()
{
    string url = @"https://some.site.com/gettoken";
    string credentials = "username=super&password=secret";
    using(var client = new HttpClient())
    {
        var response = await client.PostAsync(url, new StringContent(credentials));
        return await response.Content.ReadAsStringAsync();
    }
}

You might also be interested in System.Net.Http.FormUrlEncodedContent which allows you to pass in the parameters and their values so you don't have to construct the credentials value yourself.

More information on async/await.

Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170