C# System.Net.WebClient 奇怪地失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/791658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Net.WebClient fails weirdly
提问by Erling Paulsen
I am trying to download some data from the reporting services instance on our TFS server.
Given that the code should run on a computer that is not domain-joined, I figured that I would set the credentials myself. No luck, got a HTTP 401 Unauthorized back. Ok, so I hooked up Fiddler to see what was happening.
我正在尝试从我们的 TFS 服务器上的报告服务实例下载一些数据。
鉴于代码应该在未加入域的计算机上运行,我想我会自己设置凭据。不走运,得到了 HTTP 401 Unauthorized 回复。好的,所以我连接了 Fiddler 看看发生了什么。
But that's when I got Heisenberged- the call now went through without a hitch. So the authentication goes through with Fiddler connected, but fails without it. Is the Webclient broken or am I missing something profound here?
但那是我得到海森堡的时候——电话现在顺利通过了。因此,身份验证在 Fiddler 连接的情况下通过,但没有它就会失败。是 Web 客户端坏了还是我在这里遗漏了一些深刻的东西?
private void ThisWorksWhenDomainJoined()
{
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultNetworkCredentials;
wc.DownloadString("http://teamfoundationserver/reports/........"); //Works
}
private void ThisDoesntWork()
{
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("username", "password", "domain");
wc.DownloadString("http://teamfoundationserver/reports/........"); //blows up wih HTTP 401
}
采纳答案by Robert Vukovi?
Take a look at this link:
HTTP Authorization and .NET WebRequest, WebClient Classes
看看这个链接:
HTTP Authorization and .NET WebRequest, WebClient Classes
I had the same problem as you. I have only added one line and it started to work. Try this
我和你有同样的问题。我只添加了一行,它就开始工作了。尝试这个
private void ThisDoesntWork()
{
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("username", "password", "domain");
//After adding the headers it started to work !
wc.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
wc.DownloadString("http://teamfoundationserver/reports/........"); //blows up wih HTTP 401
}
回答by travis
What happens when you use this?
当你使用它时会发生什么?
wc.Credentials = CredentialCache.DefaultCredentials;
Also, are you sure you have the correct username, password and domain?
另外,您确定您拥有正确的用户名、密码和域吗?
Also: I wonder if Fiddler is changing around some unicode characters when .net breaks them or something like that. If your user/pass/domain has unicode, try escaping it out like "\u2638"
instead of "?"
.
另外:我想知道当 .net 破坏它们或类似的东西时,Fiddler 是否正在改变一些 unicode 字符。如果您的用户/密码/域具有 unicode,请尝试将其转义,"\u2638"
而不是"?"
.
回答by JP Alioto
Try this ...
尝试这个 ...
var credCache = new CredentialCache();
credCache.Add(new Uri("http://teamfoundationserver/reports/........""),
"Basic",
new NetworkCredential("username", "password", "DOMAIN"));
wc.Credentials = credCache;
If that does not work, try replacing "Basic" with "Negotiate".
如果这不起作用,请尝试将“基本”替换为“协商”。
回答by Guy Starbuck
I was able to get around this error by using a CredentialCache object, as follows:
我能够通过使用 CredentialCache 对象来解决这个错误,如下所示:
WebClient wc = new WebClient();
CredentialCache credCache = new CredentialCache();
credCache.Add(new Uri("http://mydomain.com/"), "Basic",
new NetworkCredential("username", "password"));
wc.Credentials = credCache;
wc.DownloadString(queryString));