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

发布于 2024-08-17 13:34:31 字数 794 浏览 5 评论 0原文

我正在尝试获取某些网页的html代码, 我的用户名和密码是正确的,但我仍然无法让它工作, 这是我的代码:

private void buttondownloadfile_Click(object sender, EventArgs e)
{
    NetworkCredentials nc = new NetworkCredentials("?", "?", "http://cdrs.globalpopsvoip.com/0000069/20091229/20091228_20091228.CDR");   
    WebClient client = new WebClient();

    client.Credentials = nc;
    String htmlCode = client.DownloadString("http://cdrs.globalpopsvoip.com/0000069/20091229/20091228_20091228.CDR");

    MessageBox.Show(htmlCode);
}

MessageBox 只是为了测试它, 问题是每次我到达这一行:

String htmlCode = client.DownloadString("http://cdrs.globalpopsvoip.com/0000069/20091229/20091228_20091228.CDR");

我都会遇到异常:

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

我该如何解决这个问题?

I'm trying to get the html code of certain webpage,
I have a username and a password that are correct but i still can't get it to work,
this is my code:

private void buttondownloadfile_Click(object sender, EventArgs e)
{
    NetworkCredentials nc = new NetworkCredentials("?", "?", "http://cdrs.globalpopsvoip.com/0000069/20091229/20091228_20091228.CDR");   
    WebClient client = new WebClient();

    client.Credentials = nc;
    String htmlCode = client.DownloadString("http://cdrs.globalpopsvoip.com/0000069/20091229/20091228_20091228.CDR");

    MessageBox.Show(htmlCode);
}

The MessageBox is just to test it,
the problem is that every time I get to this line:

String htmlCode = client.DownloadString("http://cdrs.globalpopsvoip.com/0000069/20091229/20091228_20091228.CDR");

I get an exception:

The remote server returned an error:
(401) Unauthorized.

How do I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

油饼 2024-08-24 13:34:31

就我而言,client.UseDefaultCredentials = true; 成功了。

In my case client.UseDefaultCredentials = true; did the trick.

他不在意 2024-08-24 13:34:31

我已经尝试过以下代码并且它有效。

    private void Form1_Load(object sender, EventArgs e)        
    {
        try
        {
            // Create Request
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"http://192.168.0.181/axis-cgi/com/ptz.cgi?move=up");

            // Create Client
            WebClient client = new WebClient();

            // Assign Credentials
            client.Credentials = new NetworkCredential("root", "a");

            // Grab Data
            string htmlCode = client.DownloadString(@"http://192.160.0.1/axis-cgi/com/ptz.cgi?move=up");

            // Display Data
            MessageBox.Show(htmlCode);
        }
        catch (WebException ex) 
        {
            MessageBox.Show(ex.ToString());
        }
    }

I have tried the following code and it is working.

    private void Form1_Load(object sender, EventArgs e)        
    {
        try
        {
            // Create Request
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"http://192.168.0.181/axis-cgi/com/ptz.cgi?move=up");

            // Create Client
            WebClient client = new WebClient();

            // Assign Credentials
            client.Credentials = new NetworkCredential("root", "a");

            // Grab Data
            string htmlCode = client.DownloadString(@"http://192.160.0.1/axis-cgi/com/ptz.cgi?move=up");

            // Display Data
            MessageBox.Show(htmlCode);
        }
        catch (WebException ex) 
        {
            MessageBox.Show(ex.ToString());
        }
    }
柠檬色的秋千 2024-08-24 13:34:31

尝试创建一个没有该域部分的NetworkCredential

NetworkCredential nc = new NetworkCredential("?", "?");   

Try to create a NetworkCredential without that domain part:

NetworkCredential nc = new NetworkCredential("?", "?");   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文