1

I am able to make the following web request via an jQuery AJAX call:

params = { "session[email]": "email@email.com", "session[password]": "password" }
var token;
$.ajax({
    type: "POST",
    url: "https://api.publicstuff.com:443/app/auth/sign_in",
    dataType: 'json',
    async: false,
    beforeSend: function (xhr) {
        xhr.setRequestHeader ("Authorization", "Token token=123456" );
    },
    data: params,
    success: function (response) {
        if (response.success) {
            alert('Authenticated!');
            token = response.token;
      }
});

When trying to make the same call from c# I am unable to successfully contact the remote server as I am receiving errors stating that the remote server cannot be found. Here is the code that I'm trying to get working in c#:

 var client = new HttpClient();
        client.DefaultRequestHeaders.Add("Authorization", "Token token=123456");
        var pairs = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("session[email]", "email@email.com"),
            new KeyValuePair<string, string>("session[password]", "pw")
        };

        var content = new FormUrlEncodedContent(pairs);

        var response = client.PostAsync("https://api.publicstuff.com:443/app/auth/sign_in", content).Result;

Any pointers on how to make the same web service in c# or what I'm doing wrong here?

ghoston3rd
  • 129
  • 2
  • 5
  • 14

3 Answers3

1

In C# code you are making a GET request, use HttpClient class to make a POST request instead

Dzung Cao
  • 55
  • 3
  • See the changes I made to the original post to use HttpClient. I am now getting a not found response. See anything else that is off? – ghoston3rd Mar 21 '16 at 12:04
  • Because you call PostAsync, you should add await to your command like this var response = await client.PostAsync("https://api.publicstuff.com:443/app/auth/sign_in", content).Result; – Dzung Cao Mar 22 '16 at 03:11
0

The big difference between your two ways is that the former makes the http request from your machine, whereas the c# code makes it from the server running your app.

It is quite possible that your machine can resolve api.publicstuff.com, but that the machine running the c# code cannot (Behind a firewall, or no public access at all).

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • How would I go about troubleshooting that? – ghoston3rd Mar 21 '16 at 12:16
  • @ghoston3rd log on to the box running your c# code, and try navigating in a browser to api.publicstuff.com. If you get a timout, or a DNS failure then problem diagnosed. – Jamiec Mar 21 '16 at 12:19
  • The c# code is running on the same machine that I run the Javascript Ajax call. – ghoston3rd Mar 21 '16 at 12:20
  • @ghoston3rd - are you sure. If your machine is both executing the ajax call, and the c# code then you couldnt get `remote server cannot be found` from one and not the other (except possibly some funky firewall setup allowing specific apps and not others) – Jamiec Mar 21 '16 at 12:21
  • I am positive. Both are running locally on my machine. The ajax request works like a charm. – ghoston3rd Mar 21 '16 at 12:23
  • @ghoston3rd - sorry, Im out of suggestions then. Either the address can be resolved from your machine or it can't. I don't see how one program would resolve it (javascript/browser) and the other (c#) can't. My last suggestions is speak to whoever administers the network you're on and ask them to help you diagnose the problem (they can often sniff packets to determine why an address is not resolving). – Jamiec Mar 21 '16 at 13:40
0

I got this working using RestSharp. Here is the code that worked for me:

var client = new RestClient("https://api.publicstuff.com:443/app/auth/sign_in");
var request = new RestRequest(Method.POST);

request.AddParameter("session[email]", "email@email.com"); 
request.AddParameter("session[password]", "password");
request.AddHeader("Authorization", "Token token=123456");
IRestResponse response = client.Execute(request);
ghoston3rd
  • 129
  • 2
  • 5
  • 14