473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

System.Net.WebClient DownloadString File Size Issue

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
1 12439
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: ptass | last post by:
Hi I've installed win2k3 sp1 on a machine where an openRead on any given file was previously working. After installation, I get a webException as follows... ..message "An exception occurred...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
6
by: | last post by:
I've written a very small ASP.NET page to scrape thousands of pages of content based on database IDs. It loops through a dataset to get the IDs. It worked well in testing but now I am getting an...
2
by: Tosco | last post by:
I read many examples with NetworkCredential and WebClient, but no one with a real http address. They all work in theory, but I wasn't able to use them in the real world. The following code should...
8
by: MaxMax | last post by:
Is it possible to tell to the WebClient to use an "automatic" encoding when doing DownloadString? The encoding of the connection is written in the header, so the WebClient should be able to sense...
0
by: larsergud | last post by:
Hi all, This code genearates a error saying "could not connect to remote server", but only by the webclient call inside the loop, eventhough it's the exact same calls? any suggestions? ...
2
by: MichaelSchoeler | last post by:
Hi, I'm having problems with the WebClient class regarding UTF-8 encoded data. When I access a specific webservice directly I can see the data arrives in corretly formatted UTF-8. But when I...
6
by: =?Utf-8?B?YmlsbCB0aWU=?= | last post by:
An authoritative source claims out-of-the-box asynchronous "WebClient's implementation is flawed" in .Net 3.0. Was it fixed in .Net 3.5 or .Net 3.5 SP1? Thank you.
0
by: alex21 | last post by:
I want to display the progress of the WebClient.DownloadString() method using a progress bar? Anyone know? Thanks Alex.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.