Finding Actual Directory WebClient is loading page from?

E

Earl Teigrob

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);

}
 
J

Joerg Jooss

Earl said:
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,
 
E

Earl Teigrob

Joerg

Thanks, this worked perfectly!

Earl

Joerg Jooss said:
Earl said:
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top