HttpWebRequest GetRequestStream error

I get an error in GetRequestStream:


            if (Program.ethernet.IsNetworkUp)
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@ "http://localhost/Smarthouse/Process/Microcontroler.php");
                request.Method = "POST";
                request.ContentType = "application/json";
                request.ContentLength = 100;
                Stream s = request.GetRequestStream();
                s.Write(BitConverter.GetBytes("{\"data1\": \"test\"}"));
                s.Close();
                WebResponse response = request.GetResponse();
                var he = response.Headers;
            }

My error is:

My php page returns “Yes” and it is working (entering url into web browser). The server is apache.

Why are you setting ContentLength to some constant and not to the actual length of the data. This can be the reason.

Try this:

if (Program.ethernet.IsNetworkUp)
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@ "http://localhost/Smarthouse/Process/Microcontroler.php");
                request.Method = "POST";
                request.ContentType = "application/json";
                byte[] postData = BitConverter.GetBytes("{\"data1\": \"test\"}");
                request.ContentLength = postData.Length;
                Stream s = request.GetRequestStream();                
                s.Write(postData);
                s.Close();
                WebResponse response = request.GetResponse();
                var he = response.Headers;
            }

Thanks, but no.


    public partial class Program
    {
        void ProgramStarted()
        {
            Debug.Print("Program Started");

            if (!ethernet_J11D.Interface.IsOpen)
                ethernet_J11D.Interface.Open();

            NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D.Interface);

            ethernet_J11D.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_J11D_NetworkUp);
            ethernet_J11D.UseDHCP();

        }

        void ethernet_J11D_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@ "http://localhost/Smarthouse/Process/Microcontroler.php");
            request.Method = "POST";
            request.ContentType = "application/json";
            byte[] postData = BitConverter.GetBytes("{\"data1\": \"test\"}");
            request.ContentLength = postData.Length;
            Stream s = request.GetRequestStream();
            s.Write(postData);
            s.Close();
            WebResponse response = request.GetResponse();
            var he = response.Headers;
        }

        void request_ResponseReceived(HttpRequest sender, HttpResponse response)
        {
            if (response.StatusCode == "202")
            {
            }
        }
    }

Error:

[quote]The thread ‘’ (0x3) has exited with code 0 (0x0).
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::connect [IP: 0000] ####
#### System.Net.Sockets.Socket::Connect [IP: 001d] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0019] ####
#### System.Net.HttpWebRequest::GetRequestStream [IP: 0008] ####
#### Gadgeteer.Modules.Module+NetworkModule::OnNetworkEvent [IP: 004d] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 0020] ####
#### SocketException ErrorCode = 10054
#### SocketException ErrorCode = 10054
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10054
#### SocketException ErrorCode = 10054
#### Exception System.Net.WebException - 0x00000000 (1) ####
#### Message: connection failed
#### System.Net.HttpWebRequest::EstablishConnection [IP: 0168] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0019] ####
#### System.Net.HttpWebRequest::GetRequestStream [IP: 0008] ####
#### Gadgeteer.Modules.Module+NetworkModule::OnNetworkEvent [IP: 004d] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 0020] ####
A first chance exception of type ‘System.Net.WebException’ occurred in System.Http.dll
A first chance exception of type ‘System.Net.WebException’ occurred in System.Http.dll
[/quote]

[quote]WSAECONNRESET10054
Connection reset by peer.
An existing connection was forcibly closed by the remote host. This normally results if the peer application on the remote host is suddenly stopped, the host is rebooted, the host or remote network interface is disabled, or the remote host uses a hard close (see setsockopt for more information on the SO_LINGER option on the remote socket). This error may also result if a connection was broken due to keep-alive activity detecting a failure while one or more operations are in progress. Operations that were in progress fail with WSAENETRESET. Subsequent operations fail with WSAECONNRESET.[/quote]

See if changing KeepAlive or Timeout on the request object makes any difference.

Sorry, nothing:


    public partial class Program
    {
        void ProgramStarted()
        {
            Debug.Print("Program Started");

            if (!ethernet_J11D.Interface.IsOpen)
                ethernet_J11D.Interface.Open();

            NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D.Interface);

            ethernet_J11D.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_J11D_NetworkUp);
            ethernet_J11D.UseDHCP();

        }

        void ethernet_J11D_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@ "http://localhost/Smarthouse/Process/Microcontroler.php");
            request.Method = "POST";
            request.ContentType = "application/json";
            request.KeepAlive = true;
            request.Timeout = 10000;
            byte[] postData = BitConverter.GetBytes("{\"data1\": \"test\"}");
            request.ContentLength = postData.Length;
            Stream s = request.GetRequestStream();
            s.Write(postData);
            s.Close();
            WebResponse response = request.GetResponse();
            var he = response.Headers;
        }

    }

Error:

[quote]The thread ‘’ (0x3) has exited with code 0 (0x0).
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::connect [IP: 0000] ####
#### System.Net.Sockets.Socket::Connect [IP: 001d] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0019] ####
#### System.Net.HttpWebRequest::GetRequestStream [IP: 0008] ####
#### Gadgeteer.Modules.Module+NetworkModule::OnNetworkEvent [IP: 004d] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 0020] ####
#### SocketException ErrorCode = 10054
#### SocketException ErrorCode = 10054
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10054
#### SocketException ErrorCode = 10054
#### Exception System.Net.WebException - 0x00000000 (1) ####
#### Message: connection failed
#### System.Net.HttpWebRequest::EstablishConnection [IP: 0168] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0019] ####
#### System.Net.HttpWebRequest::GetRequestStream [IP: 0008] ####
#### Gadgeteer.Modules.Module+NetworkModule::OnNetworkEvent [IP: 004d] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 0020] ####
A first chance exception of type ‘System.Net.WebException’ occurred in System.Http.dll
A first chance exception of type ‘System.Net.WebException’ occurred in System.Http.dll
Error invoking method “Gadgeteer.Modules.Module+NetworkModule” (check arguments to Program.BeginInvoke are correct)
[/quote]

Localhost sweet localhost.

Try real ip of your server :wink:

Lame, lame, lame, lame.
Thanks. I would break my head on this one.

By the way. It is also not working on 127.0.0.1. But it perfectly works on some domain.

@ Makla -

localhost is the “standard” name for ip address 127.0.0.1. You can name it differently too. Check:

Windows\System32\Drivers\etc\hosts