0

I tired different resources but could not find an answer to my question, if this question has been answered somewhere else then please send me the link for the answer.

I have restful service to consume, to do that I have to use Authentication first. well, that is working fine, I managed the authentication and I get the authentication token.

now when I want to use the service that I want I get The remote server returned an error: (401) Unauthorized.

There is no place in the service to use the token.

I created a simple C# program to do that it contains 2 buttons button 1 : will Authenticate user (works fine and I get the token) button 2 : will use the main service (does not work and get Unauthorized)

here is my code please advice how should I use the authentication token.

private void button1_Click(object sender, EventArgs e)
{
      var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://apps.ramm.co.nz:443/RammApi6.1/v1/authenticate/login");
      httpWebRequest.ContentType = "application/json";
      httpWebRequest.Method = "POST";

      using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
      {
                string json = "{\"database\":\"RAMM API Demo\"," +
                              " \"userName\":\"api_demo\"," +
                              "\"password\":\"thursday\"}";

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

       var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
       using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
       {
          var result = streamReader.ReadToEnd();
       }
 }

.............

private void button2_Click(object sender, EventArgs e)
{
     var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://apps.ramm.co.nz:443/RammApi6.1/v1/data/table");
     httpWebRequest.ContentType = "application/json";
     httpWebRequest.Method = "POST";

     using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
     {
                string json = "{ \"tableName\": \"carr_way\" " +
                                ", \"expandLookups\": \"False\" " +
                                ", \"getGeometry\": \"True\" " +
                                ", \"loadType\": \"Specified\" " +
                                ", \"columns\": [\"carr_way_no\", \"road_id\", \"carrway_start_m\", \"carrway_end_m\", \"start_name\", \"end_name\", \"added_on\", \"chgd_on\"] " +
                                ", \"filters\": [[{\"columnName\": \"added_on\", \"operator\": \"GreaterThan\", \"value\": \"2015-01-01\"}] " +
                                ", [{\"columnName\": \"chgd_on\", \"operator\": \"GreaterThan\", \"value\": \"2015-01-01\"}]]}";

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

      var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
      using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
      {
         var result = streamReader.ReadToEnd();
      }

   }
asmgx
  • 6,348
  • 12
  • 63
  • 109

1 Answers1

1

You would typically put the authorization in the authorization header, but depending on what type of authorization you are using, that may depend. This may be of help to you:

Setting Authorization Header of HttpClient

mp-mi
  • 631
  • 5
  • 6
  • That's just part of OPs problems... Since the backend is Ramm API the authorization part would be an authorization header as you stated. (Authorization: Bearer [token]). However. Since OP doesn't save the authorization toke there is no way of setting it in the next request. And also the URL is wrong in the second request. All this because OP didn't want to read the documentation for the API. – Sani Singh Huttunen Jun 14 '17 at 02:11