System.Net.WebClient DownloadString File Size Issue

M

Mike

I am using PowerShell to download an XML file into a string using the
DownloadString(URI) method of System.Net.WebClient object. Sample
commands below:

$Result = $Null;
$WebClient = new-object System.Net.WebClient;
$WebClient.Encoding = [System.Text.Encoding]::Default;
$WebClient.Proxy = [System.Net.WebRequest]::DefaultWebProxy;
$WebClient.Proxy.Credentials =
[System.Net.CredentialCache]::DefaultCredentials;
$Result = $WebClient.DownloadString("http://www.thomas-krause.de/
weblogs/elcaroop/rss.xml");
$Result | out-file "D:\DownloadString.xml";

This works as expected and the content of the original file seems to
be exactly the same as the downloaded file. The issue I am having is
that the file retrieved using $WebClient.DownloadString is
approximately twice the size of the original file (downloaded by doing
a "Save Target As..." when right clicking on the URL).

Can anyone explain why this is happening?
I have tried changing the System.Text.Encoding setting to all 7 types
that are available, some of them then give the correct file size that
I expect but the data is modified, while others give me the data that
I expect but not the file size that I expect.

The $webClient.DownloadFile("http://www.thomas-krause.de/weblogs/
elcaroop/rss.xml", "D:\DownloadedFile.xml");
method works fine for me and I get the exact data and file size as the
original but I would perfer to use DownloadString to avoid working
with local copies of the file.

Mike
 
G

Guest

Try the DownloadData method and convert the resulting byte array to a string
using (e.g) System.Text.Encoding.UTF8.GetString
and see if that doesn't help. You supply the desired encoding.
Peter
 

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

Top