Correct datasize from webrequest

  • Thread starter Finn Stampe Mikkelsen
  • Start date
F

Finn Stampe Mikkelsen

Hi

I'm trying to track the progress of the stream handling shown in the code
below. I get the correct contentlength (compared to the filesize on the
server), but as
shown below, i need to adjust the pos i report to the progressbar for it to
update correct. Any ideas on why and how i can do it correctly, since i
suspect i'm doing something wrong..

=================

WebRequest requestHtml =
WebRequest.Create("http://www.hamspirit.dk/DaffReport.csv");

WebResponse responseHtml = requestHtml.GetResponse();

StreamReader r = new
StreamReader(responseHtml.GetResponseStream(), Encoding.Default);

StringBuilder htmlContent = new StringBuilder();

pBar1.Maximum = (int)responseHtml.ContentLength;
Count = (int)responseHtml.ContentLength;

Idx = 0;

while (!r.EndOfStream)
{
path = r.ReadLine() + (char)13;
Idx += (path.Length - 1) * 2 + 4;
htmlContent.Append(path);
pBar1.Value = Idx;

}
==============

/Finn

--
Der er 10 slags mennesker - Dem som forstår binær og dem som ikke gør.
There are 10 kinds of people. Those who understand binary and those who
don't.
Es gibt 10 Arten von Menschen. Die, die Binär verstehen, bzw. die, die es
nicht tuhen.
 
A

Arne Vajhøj

I'm trying to track the progress of the stream handling shown in the
code below. I get the correct contentlength (compared to the filesize on
the server), but as
shown below, i need to adjust the pos i report to the progressbar for it
to update correct. Any ideas on why and how i can do it correctly, since
i suspect i'm doing something wrong..

=================

WebRequest requestHtml =
WebRequest.Create("http://www.hamspirit.dk/DaffReport.csv");

WebResponse responseHtml = requestHtml.GetResponse();

StreamReader r = new StreamReader(responseHtml.GetResponseStream(),
Encoding.Default);

StringBuilder htmlContent = new StringBuilder();

pBar1.Maximum = (int)responseHtml.ContentLength;
Count = (int)responseHtml.ContentLength;

Idx = 0;

while (!r.EndOfStream)
{
path = r.ReadLine() + (char)13;
Idx += (path.Length - 1) * 2 + 4;
htmlContent.Append(path);
pBar1.Value = Idx;

}
==============

Most web servers will send the lines separated by CR LF
not just CR.

Arne
 
F

Finn Stampe Mikkelsen

Arne Vajhøj said:
Most web servers will send the lines separated by CR LF
not just CR.

Arne

Hi Arne

I get that, but here it seems like the total sum of path.Length is only
around half of the responseHtml.ContentLength reported by the web-server and
that length is actually the size of the file shown in the ftp program.

Also i don't get the 4 bytes i need to add too...

/Finn
 
F

Finn Stampe Mikkelsen

The above code is followed by

r.Close();
Update = htmlContent.ToString();
SelIdx = htmlContent.Length;

And if you compare the SelIdx with responseHtml.ContentLength, you get a
ratio of approx. 1-2... SelIdx is a little less than half the value of
responseHtml.ContentLength... That is what seriosly puzzles me and make me
wonder where i go wrong. Where does this difference occur..

Is it in the stream?? I have checked Update's content and it does hold all
the data. No data is missing.. Is the size reported by
responseHtml.ContentLength (which i interpret as the filesize actually
different than the effective size of the data contained by the file??

/Finn
 
A

Arne Vajhøj

The above code is followed by

r.Close();
Update = htmlContent.ToString();
SelIdx = htmlContent.Length;

And if you compare the SelIdx with responseHtml.ContentLength, you get a
ratio of approx. 1-2... SelIdx is a little less than half the value of
responseHtml.ContentLength... That is what seriosly puzzles me and make
me wonder where i go wrong. Where does this difference occur..

Is it in the stream?? I have checked Update's content and it does hold
all the data. No data is missing.. Is the size reported by
responseHtml.ContentLength (which i interpret as the filesize actually
different than the effective size of the data contained by the file??

Have you accounted for the difference between bytes and chars?

responseHtml.ContentLength er bytes og htmlContent.Length er chars.

Arne


Arne
 
F

Finn Stampe Mikkelsen

Arne Vajhøj said:
Have you accounted for the difference between bytes and chars?

responseHtml.ContentLength er bytes og htmlContent.Length er chars.

Arne

Argh... Ofcourse.. That would explain it, since char is 16bit vs bytes being
8... But how does the extra 4 (bits) come in?? it would seem as if i have to
put in extra 4 every time

/Finn
 
A

Arne Vajhøj

Argh... Ofcourse.. That would explain it, since char is 16bit vs bytes
being 8... But how does the extra 4 (bits) come in?? it would seem as if
i have to put in extra 4 every time

The relationship between chars and bytes depends on the encoding.

UTF-16 1:2
UTF-8 typical 1:1.05
ISO-8859-n 1:1

Arne
 
F

Finn Stampe Mikkelsen

The relationship between chars and bytes depends on the encoding.

UTF-16 1:2
UTF-8 typical 1:1.05
ISO-8859-n 1:1

Arne

Hi Arne

Yeah, i've been reading up on it.. Can see the light now, but i'm still
baffled over the extra 4 bit.. Can't finde any explanation for that...

Anyway, thanks for your clarification on the subject. i keept our talk in
english in respect for this forum, even though we both come from Denmark..

Have a nice weekend...

Cheers

/Finn
 
A

Arne Vajhøj

Yeah, i've been reading up on it.. Can see the light now, but i'm still
baffled over the extra 4 bit.. Can't finde any explanation for that...

What 4 bit?

Arne
 
F

Finn Stampe Mikkelsen

--
Der er 10 slags mennesker - Dem som forstår binær og dem som ikke gør.
There are 10 kinds of people. Those who understand binary and those who
don't.
Es gibt 10 Arten von Menschen. Die, die Binär verstehen, bzw. die, die es
nicht tuhen.

Arne Vajhøj said:
What 4 bit?

Arne

My calc to compensate and reach the size reported earlier is like this...

Idx += (path.Length - 1) * 2 + 4;

I get the *2 due to the encoding, but not the + 4...

/Finn
 
A

Arne Vajhøj

My calc to compensate and reach the size reported earlier is like this...

Idx += (path.Length - 1) * 2 + 4;

I get the *2 due to the encoding, but not the + 4...

CRLF in UTF-16 ?

Just guessing.

Arne
 
F

Finn Stampe Mikkelsen

Arne Vajhøj said:
CRLF in UTF-16 ?

Just guessing.

Arne

I'm sure it's correct regarding the UTF-16, but CRLF would constitute a
#10#13 in char egual 32 bits in UTF-16 if i'm correct. Anyway, it can't be ½
a byte..

Never mind Arne. I've gotten hugely smarter and my basic question
answered...

/Finn
 
A

Arne Vajhøj

I'm sure it's correct regarding the UTF-16, but CRLF would constitute a
#10#13 in char egual 32 bits in UTF-16 if i'm correct. Anyway, it can't
be ½ a byte..

That 4 sounds like bytes not bits to me.

Arne
 
F

Finn Stampe Mikkelsen

Arne Vajhøj said:
That 4 sounds like bytes not bits to me.

Arne

You might be right on that one and would suggest the #10#13 scenario to be
the correct one...

Thanks again... Have a nice weekend..

/Finn
 

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