473,327 Members | 1,892 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,327 software developers and data experts.

Finding Actual Directory WebClient is loading page from?

I am loading a page using WebClient using the code below. The thing is that
on a redirect, I do not end up knowing where the page was loaded from. I
need this information because I am parsing the downloaded HTML page and
replacing Relative references with Absolute references. I have read that
this can be accomplished using HttpWebRequest but have not idea how to
implement it to retrieve the final destination directory. If anyone has the
actual code snippet to do this, it would be a great help.

Thanks in Advance

Earl
private string HtmlPageContent(string PageUrl)

{

WebClient MyWebClient = new WebClient();

byte[] bPageHtml = MyWebClient.DownloadData(PageUrl);

System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

return enc.GetString(bPageHtml);

}
Nov 18 '05 #1
2 1638
Earl Teigrob wrote:
I am loading a page using WebClient using the code below. The thing
is that on a redirect, I do not end up knowing where the page was
loaded from. I need this information because I am parsing the
downloaded HTML page and replacing Relative references with Absolute
references. I have read that this can be accomplished using
HttpWebRequest but have not idea how to implement it to retrieve the
final destination directory. If anyone has the actual code snippet to
do this, it would be a great help.

Thanks in Advance


Not a big deal -- you send your request using (Http)WebRequest and check the
WebResponse's ResponseUri property:

// Method to download a (textual) web resource, UTF-8 encoding assumed.

public ResponseDto GetPage(Uri uri) {
WebRequest request = WebRequest.Create(uri);
WebResponse response = request.GetResponse();
string responseSource = null;
Uri responseUri = null;

using (Stream responseStream = response.GetResponseStream()) {
MemoryStream memoryStream = new MemoryStream(0x10000);
byte[] buffer = new byte[0x1000];
int bytes;
while ((bytes = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Write(buffer, 0, bytes);
}
responseSource = Encoding.UTF8.GetString(buffer);
responseUri = response.ResponseUri;
}
return new ResponseDto(responseSource, responseUri);
}

// Data transfer object

public struct ResponseDto {
private string source;
private Uri uri;

public ResponseDto(string source, Uri uri) {
this.source = source;
this.uri = uri;
}

public string Source {
get { return this.source; }
}

public Uri Uri {
get { return this.uri; }
}
}

Cheers,

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

Nov 18 '05 #2
Joerg

Thanks, this worked perfectly!

Earl

"Joerg Jooss" <jo*********@gmx.net> wrote in message
news:uR**************@TK2MSFTNGP11.phx.gbl...
Earl Teigrob wrote:
I am loading a page using WebClient using the code below. The thing
is that on a redirect, I do not end up knowing where the page was
loaded from. I need this information because I am parsing the
downloaded HTML page and replacing Relative references with Absolute
references. I have read that this can be accomplished using
HttpWebRequest but have not idea how to implement it to retrieve the
final destination directory. If anyone has the actual code snippet to
do this, it would be a great help.

Thanks in Advance
Not a big deal -- you send your request using (Http)WebRequest and check

the WebResponse's ResponseUri property:

// Method to download a (textual) web resource, UTF-8 encoding assumed.

public ResponseDto GetPage(Uri uri) {
WebRequest request = WebRequest.Create(uri);
WebResponse response = request.GetResponse();
string responseSource = null;
Uri responseUri = null;

using (Stream responseStream = response.GetResponseStream()) {
MemoryStream memoryStream = new MemoryStream(0x10000);
byte[] buffer = new byte[0x1000];
int bytes;
while ((bytes = responseStream.Read(buffer, 0, buffer.Length)) > 0) {
memoryStream.Write(buffer, 0, bytes);
}
responseSource = Encoding.UTF8.GetString(buffer);
responseUri = response.ResponseUri;
}
return new ResponseDto(responseSource, responseUri);
}

// Data transfer object

public struct ResponseDto {
private string source;
private Uri uri;

public ResponseDto(string source, Uri uri) {
this.source = source;
this.uri = uri;
}

public string Source {
get { return this.source; }
}

public Uri Uri {
get { return this.uri; }
}
}

Cheers,

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

Nov 18 '05 #3

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

Similar topics

0
by: Helen Abell | last post by:
Hi, I am trying to use a .Net WebClient object to read data from a url, but I am getting the following error: System.Net.HttpWebRequest.CheckFinalStatus()...
1
by: Nurchi BECHED | last post by:
Hello, everyone. I am stuck with the following: There is a webpage which contains a webform, which contains text fields, and submits the data from current page to another one. I need to...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
0
by: Helen | last post by:
Hi, I am trying to use a .Net WebClient object to read data from a url, but I am getting the following error: System.Net.HttpWebRequest.CheckFinalStatus()...
1
by: Scampi | last post by:
Hi, I've been working on an application to do some 'scraping' of web content, using the WebClient class. I'm using code rather like the following.. Dim objWebClient As New WebClient Dim...
4
by: ewolfman | last post by:
Hi, This may sound a little complicated, but here goes: I have 2 webforms (lets call them 'x' and 'y'). * - 'x' is a form which contains a single server side TextBox web control, and an...
1
by: Tim Dot NoSpam | last post by:
I saw an article a while back somewhere that talked about connecting to a web page through WebClient then somehow loading the page into something that does the HTML parsing for you. I usually...
5
by: GenCode | last post by:
What is the best way to read a "readable" web directory... I know I can do this Client.DownloadFile("http://www.mydomain.com/readabledir/", c:\ \dir.txt"); But that gives me the html and all...
7
by: Nick | last post by:
Hi there, I have a website that functions fine locally, but when published to the server it develops a bottleneck during loading some of the pages. Basically what happens is the page loads to...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.