System.Net.WebClient fails weirdly

前端 未结 4 816
梦谈多话
梦谈多话 2021-02-19 18:29

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 t

4条回答
  •  Happy的楠姐
    2021-02-19 18:54

    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
        }
    

提交回复
热议问题