The remote server returned an error: (401) Unauthorized
Asked Answered
S

3

12

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?

Sovran answered 10/1, 2010 at 21:56 Comment(0)
M
17

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

Microminiaturization answered 23/6, 2013 at 7:29 Comment(0)
S
7

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());
        }
    }
Santee answered 7/5, 2010 at 7:36 Comment(0)
C
3

Try to create a NetworkCredential without that domain part:

NetworkCredential nc = new NetworkCredential("?", "?");   
Cabaret answered 10/1, 2010 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.