user1279173
user1279173

Reputation: 129

Get 401 unauthorized web exception when consuming rest json api in c#

i have bin trying to consume a rest json api with a get request but i always 401 unauthorized exception. According to the owners of the web service they are using http basic authentication. I have tried the credentials by logging in through chrome so they are working fine.

var synCClient = new WebClient();
synCClient.Credentials = new NetworkCredential("user", "password");
var content = synCClient.DownloadString(url);

I have also tried this solution which i found in kowalczyks blog

string authInfo = userName + ":" + passWord;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Headers["Authorization"] = "Basic" + authInfo;

Any thoughts

Upvotes: 1

Views: 1088

Answers (1)

Anirudha
Anirudha

Reputation: 32797

Shouldn't it be:

req.Headers["Authorization"] = "Basic " + authInfo;
                                     ^

You seem to have missed space after "Basic"

Upvotes: 1

Related Questions