Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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#_.net_Http Status Code 401 - Fatal编程技术网

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

C# 远程服务器返回错误:(401)未经授权,c#,.net,http-status-code-401,C#,.net,Http Status Code 401,我正在尝试获取某个网页的html代码, 我有一个正确的用户名和密码,但我仍然无法让它工作, 这是我的代码: private void buttondownloadfile_Click(object sender, EventArgs e) { NetworkCredentials nc = new NetworkCredentials("?", "?", "http://cdrs.globalpopsvoip.com/0000069/20091229/20091228_20091228.

我正在尝试获取某个网页的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)未经授权


如何修复此问题?

尝试创建一个没有该域部分的
网络凭据

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

我已经尝试了以下代码,它正在工作

    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());
        }
    }

在我的例子中,
client.UseDefaultCredentials=true成功了