FileStream fOut = File.Create(@"\SD\downed"); HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://example.com");//"http://feeds2.feedburner.com/the404/"); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream respStream = resp.GetResponseStream(); int read = respStream.ReadByte(); while (read != -1) { read = respStream.ReadByte(); fOut.WriteByte((byte)read); } respStream.Close(); fOut.Close();

MF alternate to Stream.CopyTo
Started by spaceyguy, Feb 01 2011 11:03 PM
7 replies to this topic
#1
Posted 01 February 2011 - 11:03 PM
I was trying to duplicate the effect of using the Stream.CopyTo, so that I could redirect the output of a HttpWebResponse response stream to a FileStream. I created the following to as an attempt; it works on regular .Net but not on the netduino+.
#2
Posted 01 February 2011 - 11:36 PM
Hi spaceyguy,
What is the result if you try doing that with .NET MF?
Chris
#3
Posted 01 February 2011 - 11:56 PM
I'm trying to download the requested page.
#4
Posted 02 February 2011 - 12:00 AM
Sorry, let me ask that another way...what error are you getting when you run the code? Is a feature not implemented? Do you get an exception?
#5
Posted 02 February 2011 - 05:44 AM
Whatever other issues it may have, it also seems to have a bug, as it unconditionally throws away the first byte read without copying it. You probably want:
while(true) { int read = respStream.ReadByte(); if(read == -1) break; fOut.WriteByte((byte)read); }
#6
Posted 02 February 2011 - 07:12 PM
Thanks Corey, I knew there was a better design but I was having a mental block preventing me to find one.
Chris as for what actually happens, after 5 minutes I get the following:
Chris as for what actually happens, after 5 minutes I get the following:
The thread '<No Name>' (0x2) has exited with code 0 (0x0). #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) #### #### Message: #### Microsoft.SPOT.Net.SocketNative::recv [IP: 0000] #### #### System.Net.Sockets.Socket::Receive [IP: 0018] #### #### System.Net.Sockets.NetworkStream::Read [IP: 0062] #### #### System.Net.InputNetworkStreamWrapper::RefillInternalBuffer [IP: 0038] #### #### System.Net.InputNetworkStreamWrapper::ReadByte [IP: 000e] #### #### DownFile.Program::Main [IP: 0026] #### #### SocketException ErrorCode = 10060 #### SocketException ErrorCode = 10060 A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll #### SocketException ErrorCode = 10060 An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
#7
Posted 02 February 2011 - 09:20 PM
spaceyguy,
It looks like the final ReadByte() is waiting for a timeout of 5 minutes before throwing a timeout exception of sorts.
Do you get all of your data before that call? Perhaps the ReadByte() call is improperly timing out instead of just returning EOF?
Chris
#8
Posted 03 February 2011 - 12:30 AM
It only creates the file, nothing is output into it.
EDIT: When I substituted respStream with a FileStream it did work, if that means anything.
EDIT 2: I changed the page that it requested and it was successful.
EDIT: When I substituted respStream with a FileStream it did work, if that means anything.
EDIT 2: I changed the page that it requested and it was successful.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users