2010-01-10 22 views
10

Belirli bir web sayfasının html kodunu almaya çalışıyorum, Doğru bir kullanıcı adı ve şifrem var ama hala işe yaramayacağım, Uzak sunucu bir hata verdi: (401) Yetkisiz

:

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

istisna almak:

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 sadece sorun bu çizgiye olsun her zaman yani test etmektir: bu benim kodudur

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

Bunu nasıl düzeltirim?

cevap

3

bu etki alanı kısmı olmadan bir NetworkCredential oluşturmayı deneyin:

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

Teşekkürler bu işe yaradı –

6

Aşağıdaki kodu denedim ve çalışıyor.

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

Gerçekten işe yarıyor ... Mükemmel !! :) Teşekkürler bir rahat Rahat :) – Bravo

+12

HttpWebRequest'in amacı nedir? – Nacht

13

Benim durumumda client.UseDefaultCredentials = true; hile yaptı.

İlgili konular