0

I want to download webpage code (plug.dj) and paste it into string. It is not hard but when I test my program an error appeared.

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

I think I cannot download code because I am not logged on this website. I tried add credentials into my code but I have no idea how it should looks like. User can log in with Google, Facebook or Twitter.

My code:

WebRequest request = WebRequest.Create("http://plug.dj/drum-bass/");
request.Credentials = new NetworkCredential (**Here should be username**, **Password**);
request.Method = "GET";

WebResponse response = request.GetResponse();

Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);

string content = reader.ReadToEnd();

reader.Close();
response.Close();

Know someone how to solve this?

I got idea how to solve it but I dont know if it is realisable. Maybe could this program using data of web browser and get this information by this way.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Sonyck
  • 186
  • 6

1 Answers1

0

As long as you only want to download a public/anonymous-login page of this website, I can't see any reason why you need to pass user-credentials or get 401 error code - no authentication in needed.

I just tried it at my end and it is working as desired:

using System.Net;
//...
using (WebClient client = new WebClient ()) // WebClient class inherits IDisposable
{
    string htmlCode = client.DownloadString("http://plug.dj");
}

Results:

<!doctype html><!--[if lt IE 7 ]><html lang="en" class="no-js ie6"><![endif]--><!--[if IE 7 ]><html lang="en" class="no-js ie7"><![endif]--><!--[if IE 8 ]><html lang="en" class="no-js ie8"><![endif]--><!--[if (gte IE 9)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]--><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>plug.dj &ndash;&nbsp;</title><link rel="icon" ...
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
  • Yes, but I need download page on this site where I have to be logged in. On this page are rooms and you need authentication to access them. For example http://plug.dj/drum-bass – Sonyck Feb 23 '14 at 22:06