Proper way to construct POST request?

I’m stuck with my networking module.
The issue that I’m having is that I can’t seem to make a valid request towards my website running in debug on my local computer, using my Hydra paired with the EthernetEnc28. I’ve verified DNS, gateways etc are all being set in my code, but I cannot reach my computer’s ASP.Net page, HttpResponse gives me 400 Bad Request. when I do a Dns.GetHostEntry(), it resolves to the correct IP

In debug, this shows all network values to appear correct (gateway, ip address, dns etc)

My failing code comes here:


        public void TransmitValue(string json)
        {
            using (var request = HttpWebRequest.Create(_postUri))
            {
                request.Method = "POST";
                request.ContentType = "application/json; charset=utf-8";
                request.ContentLength = json.Length;

                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(json);
                }

                try
                {
                    var response = request.GetResponse() as HttpWebResponse;
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.Message);
                }
            }
        }

Exception:


The thread '<No Name>' (0x3) 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::Read_HTTP_Line [IP: 004b] ####
    #### System.Net.HttpWebRequest::ParseHTTPResponse [IP: 002e] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 0035] ####
    #### CareForPlant.Managers.ValuesTransmitter::TransmitValue [IP: 0045] ####
    #### CareForPlant.Program::OnTimerTick [IP: 0046] ####
    #### Gadgeteer.Timer::dt_Tick [IP: 0018] ####
    #### Microsoft.SPOT.DispatcherTimer::FireTick [IP: 0010] ####
    #### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
    #### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
    #### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
    #### Gadgeteer.Program::Run [IP: 001d] ####
    #### SocketException ErrorCode = 10054
    #### SocketException ErrorCode = 10054
    #### SocketException ErrorCode = 10054
    #### Exception System.Net.WebException - 0x00000000 (1) ####
    #### Message: 
    #### System.Net.HttpWebRequest::GetResponse [IP: 00c8] ####
    #### CareForPlant.Managers.ValuesTransmitter::TransmitValue [IP: 0045] ####
    #### CareForPlant.Program::OnTimerTick [IP: 0046] ####
    #### Gadgeteer.Timer::dt_Tick [IP: 0018] ####
    #### Microsoft.SPOT.DispatcherTimer::FireTick [IP: 0010] ####
    #### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
    #### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
    #### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
    #### Gadgeteer.Program::Run [IP: 001d] ####
Exception thrown: 'System.Net.WebException' in System.Http.dll

I’ve tried addressing my ASP.Net site as:
http://:8080/
http://:8080/

When I do the request with pure IP instead of hostname, I do not get the exception, but I still get “Bad Request”.

On my website in debug, I’ve set a breakpoint in the Application_BeginRequest() to see if anything at all reaches it. It doesen’t.

This probably has a very simple solution, I’m just not seeing it. Oh, and my SDK is 4.3, running on Hydra 1.2.

This is a basic example of the complexity behind simple web-technology. I would recommend using Fiddler, which is a free tool. Fiddler | Web Debugging Proxy and Troubleshooting Solutions

With this you can try to emulate your request, and even see the request from the device.

Good luck!

Fiddler only monitors outgoing requests afaik, and setting a breakpoint on the method below will catch any type of request coming into the server:


        protected void Application_BeginRequest()
        {
            var request = HttpContext.Current.Request;
        }

meaning, if you don’t see the breakpoint happening there, nothing reached the server. I am on to something now tho, it seems as if I’m not able to browse IIS Express from anywhere but the local computer, so I’ve followed the steps in this article:

[url]http://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer[/url]

hopefully it helps.

Update - yeah, that was it. I moved my website to Azure and it instantly started to take values. It was IIS express that the Hydra was unable to see (or any other computer on my local network) will sort out those issues and then I should be good. Apologies for all the questions :slight_smile:

1 Like