473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

webpage download?

i wonder how to download a webpage automatically using a C# program
with WebClient class.
i tried it and it's working. but somehow the data it gets is different
than the page shown in microsoft internet explorer.
and i wonder how to get around the login problems. the C# programs
only downloads the login page, do i need to "instruct" the program to
access the cookie information on my window?
thankx
Nov 15 '05 #1
5 4942
Alan Zhong wrote:
i wonder how to download a webpage automatically using a C# program
with WebClient class.
i tried it and it's working. but somehow the data it gets is different
than the page shown in microsoft internet explorer.
and i wonder how to get around the login problems. the C# programs
only downloads the login page, do i need to "instruct" the program to
access the cookie information on my window?


WebClient is not sufficient to implement HTTP conversations, since it
doesn't support cookies. You must use HttpWebRequest and HttpWebResponse
instead.

Cheers,
--
Joerg Jooss
jo*********@gmx.net

Nov 15 '05 #2
hi everybody, i am still trying to get this work...
here were what i did.
1) use HttpWebRequest to connection the server with username +
password. call getResponse, successfully get the response header
containing the ["Set-Cookie"] data.
2) put ["Set-Cookie"] into the headers of a newly created request with
the website i want to download under the same protected domain.
3) call getResponose again but it still redirects me to the login
page.

what did i do wrong?
the following are the codes i have:
////////////////////////// test12.cs
using System;
using System.IO;
using System.Net;
class test12
{
static void Main(string[] args)
{
String address = "xxx login page with username and password xxx";
String newaddr = "page wanted to download";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

request = (HttpWebRequest) WebRequest.Create(newaddr);
// response headers info:
// Header Name:Server, Value :Sun-ONE-Web-Server/6.1
// Header Name:Date, Value :Thu, 01 Apr 2004 21:12:48 GMT
// Header Name:Content-type, Value :text/html
// Header Name:Set-cookie, Value :RMID=xxxxx; expires=Friday,
// 01-Apr-2005 21:12:48 GMT; path=/; domain=.xxx.com
// Header Name:Cache-control, Value :no-cache
// Header Name:Pragma, Value :no-cache
// Header Name:Transfer-encoding, Value :chunked
request.Headers.Add("Cookie", response.Headers["Set-Cookie"]);

Console.WriteLine("old Address: " + request.Address);
response = (HttpWebResponse) request.GetResponse();
Console.WriteLine("new Address: " + request.Address);
Console.WriteLine("ResponseUri " + response.ResponseUri);

StreamReader sr = new StreamReader(response.GetResponseStream());
String result = sr.ReadToEnd();
sr.Close();

response.Close();

Console.WriteLine(result.Length);
if (result.Length > 1000)
result = result.Substring(0, 1000);
Console.WriteLine(result);
}
Nov 16 '05 #3
hi everybody, i am still trying to get this work...
here were what i did.
1) use HttpWebRequest to connection the server with username +
password. call getResponse, successfully get the response header
containing the ["Set-Cookie"] data.
2) put ["Set-Cookie"] into the headers of a newly created request with
the website i want to download under the same protected domain.
3) call getResponose again but it still redirects me to the login
page.

what did i do wrong?
the following are the codes i have:
////////////////////////// test12.cs
using System;
using System.IO;
using System.Net;
class test12
{
static void Main(string[] args)
{
String address = "xxx login page with username and password xxx";
String newaddr = "page wanted to download";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

request = (HttpWebRequest) WebRequest.Create(newaddr);
// response headers info:
// Header Name:Server, Value :Sun-ONE-Web-Server/6.1
// Header Name:Date, Value :Thu, 01 Apr 2004 21:12:48 GMT
// Header Name:Content-type, Value :text/html
// Header Name:Set-cookie, Value :RMID=xxxxx; expires=Friday,
// 01-Apr-2005 21:12:48 GMT; path=/; domain=.xxx.com
// Header Name:Cache-control, Value :no-cache
// Header Name:Pragma, Value :no-cache
// Header Name:Transfer-encoding, Value :chunked
request.Headers.Add("Cookie", response.Headers["Set-Cookie"]);

Console.WriteLine("old Address: " + request.Address);
response = (HttpWebResponse) request.GetResponse();
Console.WriteLine("new Address: " + request.Address);
Console.WriteLine("ResponseUri " + response.ResponseUri);

StreamReader sr = new StreamReader(response.GetResponseStream());
String result = sr.ReadToEnd();
sr.Close();

response.Close();

Console.WriteLine(result.Length);
if (result.Length > 1000)
result = result.Substring(0, 1000);
Console.WriteLine(result);
}
Nov 16 '05 #4
hi everybody, i am still trying to get this work...
here were what i did.
1) use HttpWebRequest to connection the server with username +
password. call getResponse, successfully get the response header
containing the ["Set-Cookie"] data.
2) put ["Set-Cookie"] into the headers of a newly created request with
the website i want to download under the same protected domain.
3) call getResponose again but it still redirects me to the login
page.

what did i do wrong?
the following are the codes i have:
////////////////////////// test12.cs
using System;
using System.IO;
using System.Net;
class test12
{
static void Main(string[] args)
{
String address = "xxx login page with username and password xxx";
String newaddr = "page wanted to download";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

request = (HttpWebRequest) WebRequest.Create(newaddr);
// response headers info:
// Header Name:Server, Value :Sun-ONE-Web-Server/6.1
// Header Name:Date, Value :Thu, 01 Apr 2004 21:12:48 GMT
// Header Name:Content-type, Value :text/html
// Header Name:Set-cookie, Value :RMID=xxxxx; expires=Friday,
// 01-Apr-2005 21:12:48 GMT; path=/; domain=.xxx.com
// Header Name:Cache-control, Value :no-cache
// Header Name:Pragma, Value :no-cache
// Header Name:Transfer-encoding, Value :chunked
request.Headers.Add("Cookie", response.Headers["Set-Cookie"]);

Console.WriteLine("old Address: " + request.Address);
response = (HttpWebResponse) request.GetResponse();
Console.WriteLine("new Address: " + request.Address);
Console.WriteLine("ResponseUri " + response.ResponseUri);

StreamReader sr = new StreamReader(response.GetResponseStream());
String result = sr.ReadToEnd();
sr.Close();

response.Close();

Console.WriteLine(result.Length);
if (result.Length > 1000)
result = result.Substring(0, 1000);
Console.WriteLine(result);
}
Nov 16 '05 #5
hi everybody, i am still trying to get this work...
here were what i did.
1) use HttpWebRequest to connection the server with username +
password. call getResponse, successfully get the response header
containing the ["Set-Cookie"] data.
2) put ["Set-Cookie"] into the headers of a newly created request with
the website i want to download under the same protected domain.
3) call getResponose again but it still redirects me to the login
page.

what did i do wrong?
the following are the codes i have:
////////////////////////// test12.cs
using System;
using System.IO;
using System.Net;
class test12
{
static void Main(string[] args)
{
String address = "xxx login page with username and password xxx";
String newaddr = "page wanted to download";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

request = (HttpWebRequest) WebRequest.Create(newaddr);
// response headers info:
// Header Name:Server, Value :Sun-ONE-Web-Server/6.1
// Header Name:Date, Value :Thu, 01 Apr 2004 21:12:48 GMT
// Header Name:Content-type, Value :text/html
// Header Name:Set-cookie, Value :RMID=xxxxx; expires=Friday,
// 01-Apr-2005 21:12:48 GMT; path=/; domain=.xxx.com
// Header Name:Cache-control, Value :no-cache
// Header Name:Pragma, Value :no-cache
// Header Name:Transfer-encoding, Value :chunked
request.Headers.Add("Cookie", response.Headers["Set-Cookie"]);

Console.WriteLine("old Address: " + request.Address);
response = (HttpWebResponse) request.GetResponse();
Console.WriteLine("new Address: " + request.Address);
Console.WriteLine("ResponseUri " + response.ResponseUri);

StreamReader sr = new StreamReader(response.GetResponseStream());
String result = sr.ReadToEnd();
sr.Close();

response.Close();

Console.WriteLine(result.Length);
if (result.Length > 1000)
result = result.Substring(0, 1000);
Console.WriteLine(result);
}
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Patrick | last post by:
I am trying to download the html page at http://www.dreamteamfc.com/dtfc04/servlet/PostPlayerList?catidx=1 Using the code public class DownloadWebPage { public static void main (String...
2
by: Joey Martin | last post by:
My issue is two-fold. I need to download content on a webpage once a day and then parse the data. I know how to parse, so once I get the page, I'm ready to go. Here's my question. Is it...
0
by: ravibrl | last post by:
Hey I need c++ code to access a webpage and download some software. If I execute that it should download that software and it should install it in my system. Give suggestions how to write that code...
2
by: Mike | last post by:
Hello, I am trying to find a way to better analyze the meta tags on my website. I would like to get the source code and then parse the code for the meta tags, links, etc. Can someone give me...
1
by: I Haas | last post by:
C#, .Net framework 1.1 visual studio 2003 From a aspx page I want to open the dialog box (download;save/open/cancel) I want to close the aspx page after the dialogbox opens. I open the dialog...
1
by: smitanaik | last post by:
how to write an api to download contents from webpage
1
by: JRussell | last post by:
Hi, I am trying to download sql database through asp on my webpage and I keep getting weird outcomes in Excel. I have had to put in <char10> and <char13> into my sql data to have it display...
10
by: CJ | last post by:
Can someone tell me how to download a webpage complete with images and insert it into a richtextbox? Thanks
1
by: jambalapamba | last post by:
Hi all, I want to download images,css,js files referenced by a webpage. I am doing this by downloading the HTML of webpage and getting all the URL references in the html and using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.