在C#中使用Rest JSON API时获得401未经授权的Web异常 [英] Get 401 unauthorized web exception when consuming rest json api in c#

查看:526
本文介绍了在C#中使用Rest JSON API时获得401未经授权的Web异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有bin试图使用带有get请求的rest json api,但是我总是401未经授权的异常.根据Web服务的所有者,他们正在使用http基本身份验证.我已经尝试通过chrome登录来尝试使用凭据,因此它们可以正常工作.

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);

我也尝试了在kowalczyks博客中找到的这种解决方案

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;

任何想法

推荐答案

应该不是:

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

您似乎在基本"之后错过了空格

You seem to have missed space after "Basic"

这篇关于在C#中使用Rest JSON API时获得401未经授权的Web异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆