System.Net.WebClient古怪失败 [英] System.Net.WebClient fails weirdly

查看:359
本文介绍了System.Net.WebClient古怪失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从假定的代码应该是不加入域的计算机上运行我们的TFS服务器。

的Reporting Services实例下载一些数据,我计算过,我会成立凭据自己。没有运气,得到了一个HTTP 401未授权回来。好了,我迷上了小提琴手,看看发生了什么事。



但是,这是当我 Heisenberged - 电话现在,经历了顺利。所以认证经过与小提琴手连接,但没有失败。在Web客户端断开或我思念的东西深刻在这里?



 私人无效ThisWorksWhenDomainJoined()
{
WebClient的WC =新的WebClient();
wc.Credentials = CredentialCache.DefaultNetworkCredentials;
wc.DownloadString(HTTP:// teamfoundationserver /报告/ ........); //作品
}

私人无效ThisDoesntWork()
{
WebClient的WC =新的WebClient();
wc.Credentials =新的NetworkCredential(用户名,密码,域);
wc.DownloadString(HTTP:// teamfoundationserver /报告/ ........); //炸毁王氏HTTP 401
}


解决方案

看看这个链接:结果
HTTP授权和.NET WebRequest的,Web客户端类



我有同样的问题,因为你。我只是加入一行,并开始工作。试试这个

 私人无效ThisDoesntWork()
{
WebClient的WC =新的WebClient();
wc.Credentials =新的NetworkCredential(用户名,密码,域);
//添加它开始工作的头后!
wc.Headers.Add(HttpRequestHeader.UserAgent的Mozilla / 4.0(兼容; MSIE 6.0; Windows NT的5.1));
wc.DownloadString(HTTP:// teamfoundationserver /报告/ ........); //炸毁王氏HTTP 401
}


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.

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?

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
    }

解决方案

Take a look at this link:
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
    }

这篇关于System.Net.WebClient古怪失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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