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

System.Net.WebClient DownloadString File Size Issue

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

May 2 '07 #1
Share this Question
Share on Google+
1 Reply


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

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Mike" wrote:
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

May 2 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.