Main Site Documentation

HttpWebRequest FEZ Spider


#1

Here’s a weird one. I have other functions that do the same thing as this function…however…for some reason…this code throws an error. Not always, but when it does…it always does. Maybe the first run is okay? But then every time after the first time I get the error? Also, the function takes a long time to run, maybe 3 or 4 minutes. I don’t have any problems in a full .net visual studio console app for testing and it only takes seconds.

The catch statement which contains, “Error reading from stream reader” is where the error occurs:


public static string getifconfig()
        {
            Debug.Print("Getting ifConfig...");
            string ResponseBody = string.Empty;
            try
            {
                var request = (HttpWebRequest)WebRequest.Create("http://ifconfig.me/ip");
                request.Method = "GET";
                request.KeepAlive = false;
                try
                {
                    using (WebResponse response = request.GetResponse())
                    {
                        try
                        {
                            using (var reader = new StreamReader(response.GetResponseStream()))
                            {
                                try
                                {
                                    ResponseBody = reader.ReadToEnd();
                                    if (ResponseBody.Length > 0)
                                    {
                                        ResponseBody = ResponseBody.Substring(0, ResponseBody.Length - 1);
                                    }
                                    Debug.Print("Received ifConfig...");
                                }
                                catch (Exception e)
                                {
                                    Debug.Print(e.Message);
                                    Debug.Print("Error reading from stream reader");
                                }
                                reader.Close();
                            }
                            response.Close();
                        }
                        catch (Exception e)
                        {
                            Debug.Print(e.Message);
                            Debug.Print("Error parsing ifConfig reponse after receieved");
                        }
                    }
                }
                catch(Exception e)
                {
                    Debug.Print("Could not get ifconfig response...");
                    Debug.Print(e.Message);
                }
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
                Debug.Print("Could not get address from http://ifconfig.me/ip");
            }
            return ResponseBody;
        }
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (9) ####
#### 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::ReadInternal [IP: 00d7] ####
#### System.Net.InputNetworkStreamWrapper::Read [IP: 000d] ####
#### System.IO.StreamReader::FillBufferAndReset [IP: 0031] ####
#### System.IO.StreamReader::ReadNonSeekableStream [IP: 0020] ####
#### System.IO.StreamReader::ReadToEnd [IP: 0016] ####
#### GadgeteerApp1.Program::getComputerIPaddresses [IP: 0036] ####
#### GadgeteerApp1.Program::ReportMyPublicIP [IP: 0019] ####
#### 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
#### SocketException ErrorCode = 10060
#### Exception System.IO.IOException - 0x00000000 (9) ####
#### Message: m_stream.Read
#### System.IO.StreamReader::FillBufferAndReset [IP: 0062] ####
#### System.IO.StreamReader::ReadNonSeekableStream [IP: 0020] ####
#### System.IO.StreamReader::ReadToEnd [IP: 0016] ####
#### GadgeteerApp1.Program::getComputerIPaddresses [IP: 0036] ####
#### GadgeteerApp1.Program::ReportMyPublicIP [IP: 0019] ####
A first chance exception of type ‘System.IO.IOException’ occurred in System.IO.dll
m_stream.Read


#2

That appears to be a connection timeout.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx

My guess is you’re experiencing a bad network connection or a problem on the server side. Apparently, the timeout is long resulting in your 3-4 minutes until failure.


#3

@ ianlee74 - This was the only website that was having a problem. I’m assuming it is on the server end. I’ve replaced it with another website the provides the same service. No problems.


#4

Excellent!