Search code examples
c#jsonwebclientnetworkcredentials

Access .Json URL with userid and pwd base64 authentication


I´m trying to access a website where I can download a .Json, however the site has a userID and PWD. I have the credentials for it and the site are using Basic authentication (basich auth (base64 encoded password) ) I do not know if im on the right track or if I can access the site with userid and pwd in another way?

using (var webClient = new System.Net.WebClient())
    {
       webClient.Credentials = new NetworkCredential("username", "pwd");
       var json = webClient.DownloadString("https://tracking.com/delivery/api/fetch-transactions?fromDate=2020-01-01");
       List<Example> list = JsonConvert.DeserializeObject<List<Example>>(json);
    }

The ERROR im getting is this: enter image description here

looks like its not opening the url with userid and pwd...


Solution

  • This one worked for me:

    string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
    
    webClient.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;