簡體   English   中英

遠程服務器返回錯誤:(401)未經授權

[英]The remote server returned an error: (401) Unauthorized

我正在嘗試獲取某些網頁的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)未經授權。

我該如何解決?

在我的情況下client.UseDefaultCredentials = true; 做了伎倆。

我嘗試了以下代碼,它正在工作。

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

嘗試創建沒有該域部分的NetworkCredential

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM