By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,333 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,333 IT Pros & Developers. It's quick & easy.

Finding Actual Directory WebClient is loading page from?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.