Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 远程服务器返回错误:(401)未经授权_C#_Asp.net_.net - Fatal编程技术网

C# 远程服务器返回错误:(401)未经授权

C# 远程服务器返回错误:(401)未经授权,c#,asp.net,.net,C#,Asp.net,.net,我编写了以下代码: string _response = null; string _auth = "Basic"; Uri _uri = new Uri("http://my.domain.local/my-page.aspx"); HttpWebRequest _req = (System.Net.HttpWebRequest)WebRequest.Create("http://api.olr.com/Service.svc"); CredentialCache _cc = new Cred

我编写了以下代码:

string _response = null;
string _auth = "Basic";
Uri _uri = new Uri("http://my.domain.local/my-page.aspx");
HttpWebRequest _req = (System.Net.HttpWebRequest)WebRequest.Create("http://api.olr.com/Service.svc");
CredentialCache _cc = new CredentialCache();
HttpWebResponse _res = default(HttpWebResponse);
StreamReader _sr = default(StreamReader);

_cc.Add(_uri, _auth, new NetworkCredential("username", "password", "ttp://api.olr.com/Service.svc"));
_req.PreAuthenticate = true;
_req.Credentials = _cc.GetCredential(_uri, _auth);
var response = _req.GetResponse();

System.IO.StreamReader sr =
                new System.IO.StreamReader(_res.GetResponseStream());

//_sr = new StreamReader(_res.GetResponseStream);
_response = _sr.ReadToEnd();
_sr.Close();
但是得到:

远程服务器返回错误:(401)未经授权

你错过了“h”http://api.olr.com/Service.svc"

并设置WebProxy

System.Net.WebProxy proxy = new WebProxy("http://api.olr.com", true);
proxy.Credentials = new NetworkCredential("platinum", "01CFE4BF-11BA", "http://api.olr.com/Service.svc");
_req.Proxy = proxy;

我认为您的网络凭据构造不正确。根据,第三个属性是域,但您正在传递完整的URL。您需要从第三方了解正确的域是什么


同时,您可以尝试忽略该属性,以查看登录是否成功。

您是控制服务器还是它属于其他人?如果是前者,那么首先要查看服务器上的日志,找出返回401状态的原因。当然,我假设您已经用浏览器测试了相同的凭据,并得到了相同的结果。@DominicCronin这是第三方服务如果您尝试添加用户代理标头,某些服务器不允许没有正确的用户代理的客户端可能是
 _cc.Add(_uri, _auth, new NetworkCredential("username", "password", "ttp://api.olr.com/Service.svc"));
System.Net.WebProxy proxy = new WebProxy("http://api.olr.com", true);
proxy.Credentials = new NetworkCredential("platinum", "01CFE4BF-11BA", "http://api.olr.com/Service.svc");
_req.Proxy = proxy;