Bruno Barral Bruno Barral - 1 year ago 73
reST (reStructuredText) Question

Connecting to a REST webservice in C#

I am not familiar with REST webservices, but I am trying to get a response from one of them in a C# application.

I am trying to connect to the webservice and authenticate my application to get a token. For this, I have an URL, a login and a password.

When I call the authentication method with cURL tool, I get a « success :true» answer, followed with the token string.
But when I try to do the same with my C# code, I always get a « success :false» answer and no token.

Can somebody help me to understand what is missing in my C# code to get the correct answer ? Thank you.

The cURL request (given by the webservice owner) is:

curl -X POST -d "{\"user\":\"mylogin\",\"pwd\":\"mypassword\"}" \
-H "Content-Type: application/json" http://webserviceURL/authenticate


My code is the following : (restlogin, restpassword and resturl are three strings and get the correct values for the connection. The resulting string is obtained in the variable nammed token).

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resturl + "authenticate");
request.Method = "POST";
request.Credentials = new NetworkCredential(restlogin, restpassword);
request.ContentType = "application/json";
request.Timeout = 30000;
request.ReadWriteTimeout = 30000;
request.Accept = "application/json";
request.ProtocolVersion = HttpVersion.Version11;


HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
using (Stream respStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(respStream, Encoding.UTF8);
token = reader.ReadToEnd();
reader.Close();
reader.Dispose();
response.Close();

}
}

Answer Source

As per my comment to the question, you are not sending your credentials in the request body. With request.Credentials you're setting the Authentication Header in your HttpRequest.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resturl + "authenticate");
        request.Method = "POST";
        request.ContentType = "application/json";
        request.Timeout = 30000;
        request.ReadWriteTimeout = 30000;
        request.Accept = "application/json";
        request.ProtocolVersion = HttpVersion.Version11;

// Set your Response body
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
    string json = "{\"user\":\"" + restlogin + "\"," +
                  "\"pwd\":\"" + restpassword + "\"}";

    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();
}

HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
     using (Stream respStream = response.GetResponseStream())
     {
         StreamReader reader = new StreamReader(respStream, Encoding.UTF8);
         token = reader.ReadToEnd();
         reader.Close();
         reader.Dispose();
         response.Close();

     }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download