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