We help IT Professionals succeed at work.
Get Started

C#  Rest API

688 Views
Last Modified: 2017-11-13
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"
}
Comment
Watch Question
Senior Software Engineer
CERTIFIED EXPERT
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 12 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE