简体   繁体   中英

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

Shouldn't it be:

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

You seem to have missed space after "Basic"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM