Avatar of lankapala
lankapala

asked on 

C# Rest API

Hi,

This what vendor provided to test

Error is :

The remote server returned an error: (401) Unauthorized.

My code

string result = string.Empty;
            try
            {
                // Create Request
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"https://api.verilocation.com/public/v1/drivers?id=1093609330685010");
                req.Method = "GET";
                // Create Client
                WebClient client = new WebClient();
                req.ContentType ="application/json; charset=utf-8";
               // req.UseDefaultCredentials = true;
               // req.PreAuthenticate = true;
               // req.Credentials = CredentialCache.DefaultCredentials;

                //req.ContentLength=61;


                req.Credentials = new NetworkCredential("xxx", "xxx111");
               
                   req.Headers.Add("Authorization","Bearer  Y2xpdmUuaG9sZGVuQHN0b25laGFyZHkuY28udWs6bDBnMTV0MWM1");
             //   req.Headers.Add("Authorization","Basic Y2xpdmUuaG9sZGVuQHN0b25laGFyZHkuY28udWs6bDBnMTV0MWM1");
                req.ProtocolVersion =HttpVersion.Version11;
            
                WebResponse response = req.GetResponse();
                string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
                MessageBox.Show(responseData.ToString());




              // Assign Credentials
               
                
            }
            catch (WebException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

Open in new window



Response Class (Status 200)
ModelExample Value


GET /public/v1/drivers/{id}

{
  "Id": 0,
  "Name": "string",
  "Mobile": "string",
  "Status": 0,
  "Username": "string",
  "Password": "string",
  "Email": "string",
  "ButtonId": "string"
}



curl -X GET --header 'Accept: application/json' --header 'Authorization: Basic Y2xpdmUuaG9sZGVuQHN0b25laGFyZHkuY28udWs6bDBnMTV0MWM1' 'https://api.verilocation.com/public/v1/drivers/0'
Request URL
https://api.verilocation.com/public/v1/drivers/0
Response Body
{
  "Message": "Authorization has been denied for this request."
}
Response Code
401
Response Headers
{
  "date": "Wed, 01 Nov 2017 00:11:10 GMT",
  "www-authenticate": "Bearer",
  "server": "Microsoft-IIS/8.5",
  "x-aspnet-version": "4.0.30319",
  "x-powered-by": "ASP.NET",
  "access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS",
  "content-type": "application/json; charset=utf-8",
  "access-control-allow-origin": "*",
  "cache-control": "no-cache",
  "access-control-allow-headers": "Authorization,Content-Type,X-Requested-With, Accept",
  "content-length": "61"
}
ASP.NET.NET ProgrammingC#REST

Avatar of undefined
Last Comment
lankapala

8/22/2022 - Mon