Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

HttpWebRequest GetResponse Error


  • Please log in to reply
22 replies to this topic

#1 dottorduino

dottorduino

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationCatania, Italy

Posted 15 May 2012 - 10:50 AM

Hi Duini :)

When I call the GetResponse method, it return a null status.

This is the code

 String URI = "http://" + serverIP + "/testPoll/signalr?";            
 
 pollString = URI + "transport=longPolling";
 pollString += "&connectionId=" + _connectionID;
 pollString += "&connectionData=" + Tools.RawUrlEncode("[{\"name\":\"testPoll.Chat\",\"methods\":[\"addMessage\"]}]");                        
 pollString += "&messageID=" + 45;
 pollString += "&groups=" + Tools.RawUrlEncode("[\"testPoll.Chat.foo\"]");
 pollString += "\r\n";
            
 var request = (HttpWebRequest)WebRequest.Create(pollString);
 request.Method = "GET";
 request.KeepAlive = false;

 try
 {
  var httpResponse = (HttpWebResponse)request.GetResponse();

  [...]


When I inspect on httpResponse, I have:

ContentEncoding: 'HttpResponse.ContentEncoding threw an Exception of type 'System.NullReferenceException'
ContentType: 'HttpResponse.ContentType threw an Exception of type 'System.NullReferenceException'

and the HttpStatus is "null".

Why I obtain this null Response?

#2 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 15 May 2012 - 11:15 AM

Hi

Have you tried using Stefan's netmf toolbox http client?

As far as i understand it the standard HttpWebRequest .net dev's are familiar with, relies on streams to operate, which although technically possible in netmf is very memory intensive and so is either not implemented or max's out the available ram very quickly. However the netmf toolbox http client takes a different approach and has worked very successfully for myself including long-polling scenarios.

Nak.

#3 dottorduino

dottorduino

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationCatania, Italy

Posted 16 May 2012 - 08:06 AM

Hi

Have you tried using Stefan's netmf toolbox http client?

As far as i understand it the standard HttpWebRequest .net dev's are familiar with, relies on streams to operate, which although technically possible in netmf is very memory intensive and so is either not implemented or max's out the available ram very quickly. However the netmf toolbox http client takes a different approach and has worked very successfully for myself including long-polling scenarios.

Nak.


Hi Nak,
thank you for your reply.
This toolbox is awesome!

Anyway, when the program arrives to :

HTTP_Client.HTTP_Response Response = WebSession.Get(pollString);

the Response object is still null...

#4 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 16 May 2012 - 08:48 AM

Hi Nak,
thank you for your reply.
This toolbox is awesome!

Thanks for that! :)

Anyway, when the program arrives to :

HTTP_Client.HTTP_Response Response = WebSession.Get(pollString);

the Response object is still null...


Is it possible to share the URL that you're trying to fetch and full code so I could try to reproduce it? I recently heard about an issue with IIS web servers (http://forums.iis.net/t/1166229.aspx) which want to have the connection established too fast. Could that be the issue?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#5 dottorduino

dottorduino

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationCatania, Italy

Posted 16 May 2012 - 09:03 AM

Hi Stefan,
thank you for your quick reply.

Even if I try this simple function:
 private HTTP_Client _remoteSession;
 private IntegratedSocket _remoteSocket;
 [...]

 private void testFunction()
 {
  _remoteSocket = new IntegratedSocket("localhost", 80);
  _remoteSession = new HTTP_Client(_remoteSocket);
            
  HTTP_Client.HTTP_Response response = _remoteSession.Get("");
  [...]


When the .Get(...) method is called, I encounter this message:
An unhandled exception of type 'System.NullReferenceException' occurred in System.dll

The problem (for the debugger) is in IntegratedSocket.cs at line 75.

#6 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 16 May 2012 - 09:08 AM

Hi Stefan,
thank you for your quick reply.

Even if I try this simple function:

  _remoteSocket = new IntegratedSocket("localhost", 80);
  _remoteSession = new HTTP_Client(_remoteSocket);  
  HTTP_Client.HTTP_Response response = _remoteSession.Get("");

Hi,

For localhost it makes sense, since localhost refers to the Netduino itself, which doesn't listen on port 80. Also a GET request (almost) always starts with a /

My own webserver at www.netmftoolbox.com is optimised to rule things out, and if you request /helloworld/ you'll get a few lines of text. Could you try that out?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#7 dottorduino

dottorduino

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationCatania, Italy

Posted 16 May 2012 - 09:25 AM

For localhost it makes sense, since localhost refers to the Netduino itself, which doesn't listen on port 80.


...this was the problem :mellow:

I'm porting from a Console Application where, of course, I refer to my IIS using localhost and I forgot this "little" difference between localhost on a PC and localhost on simulator -.-

Thank you for your support.

#8 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 16 May 2012 - 10:09 AM

Hmmmm intresting

I assume you are trying to connect to a ASP.Net MVC site from the signalr refference in your URI?

I had some issues with in the past due to incorrect mimetypes, doesnt signalr require a mime of "application/json" or similar?

The easiest way to prove this would be to define a hello world controller, something like this:

using System;
using System.Globalization; 
using System.Linq; 
using System.Text; 
using System.Web.Mvc;
namespace testPoll {  
 public class HomeController : Controller {    
  public string Index() {
	return "Hello World";
  }
 }
}

That controller should respond to practically anything as long as your request is well formed.

Here is some code that i have used to test the http client with before:


private void testHttpGet(){
 var ServerName = "google.com";
 var HttpClient webClient = new HttpClient(new IntegratedSocket(ServerName, 80)); }
 var response = WebClient.Get("/");
 if(response.ResponseCode != 200) {//Something went horribly wrong in the ether
  Debug.Print("FARK!!!!");
  continue;
 } 
 //HTTP Sucess parse response
 if(response.ResponseBody == "\r\n\r\n"){//Empty Response Body Discard
  continue;
 }
 Debug.Print(response.ResponseBody);
}


Nak.

#9 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 16 May 2012 - 10:21 AM

Damn phone support call at work, looks like a lot happened in the thread whilst i was on phone lol :) Glad to hear its sorted now. Funny that was the issue spent most of yesterday educating one of the Jnr Dev's here about when to use localhost and how machine name is the identifier to rely on, when sending links people for support :rolleyes: @Stefan, is their a use case for adding additional info to the XML comment for Simple socket that entering localhost here means you are connecting to the NETMF Board the client is running on, or similar words to that effect? Nak.

#10 dottorduino

dottorduino

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationCatania, Italy

Posted 17 May 2012 - 08:18 AM


@Stefan, is their a use case for adding additional info to the XML comment for Simple socket that entering localhost here means you are connecting to the NETMF Board the client is running on, or similar words to that effect?

Nak.


Just for dummies :)

Now I'm fighting with the CLR_E_OUT_OF_RANGE error at runtime!!! :angry:
Maybe the string for the request is too long?

...a few minutes later...

Solved removing the "\r\n" line from my string.

#11 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 05 December 2012 - 07:39 AM

Hi guys,

How would I check for "GetResponse" when using SimpleSocket for WiFly?


SimpleSocket Socket = new WiFlySocket("www.someSite.com", 80, WifiModule);

Socket.Connect();

Socket.Send("GET /default.aspx?var=something HTTP/1.1\r\n");
Socket.Send("Host: " + Socket.Hostname + "\r\n");
Socket.Send("Connection: Close\r\n");
Socket.Send("\r\n");

if(Socket.ResponseCode != 200)
{
  Debug.Print("Error:" + Socket.ResponseCode);
} 

Socket.Close();


Thanks,
Donovan

#12 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 05 December 2012 - 08:50 AM

You can wrap the HTTP Client around the WiFlySocket like this:
SimpleSocket Socket = new WiFlySocket("www.someSite.com", 80, WifiModule);
var HTTP_Client webClient = new HTTP_Client(Socket);

"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#13 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 05 December 2012 - 09:17 AM

Thanks Stefan... and which namespace is HttpClient in?

#14 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 05 December 2012 - 11:26 AM

Thanks Stefan... and which namespace is HttpClient in?


http://netmftoolbox....ailable classes

:)
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#15 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 05 December 2012 - 03:14 PM

Thanks Stefan ;)

#16 marksmanaz

marksmanaz

    Member

  • Members
  • PipPip
  • 15 posts

Posted 27 May 2013 - 05:07 AM

oops



#17 marksmanaz

marksmanaz

    Member

  • Members
  • PipPip
  • 15 posts

Posted 27 May 2013 - 05:16 AM

Stefan,

  I could use your help. I have been trying to use your HttpClient

 all day and can't get it to post to my IIS server and get a response. If you leave your test server in on port 80 it works fine but any other server it does nothing and eventualy I get this error:

 

"A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll"

 

When I check the logs on the IIS server it shows no requests being made by the Netduino. I can access the page from any browser with no problem.

Any help is appreciated.

 

Thank,

 

Mark

   Public Sub PostSmsMsg()        Try            ' Creates a new web session            Dim WebSession As HTTP_Client = New HTTP_Client(New IntegratedSocket("http://XXXXXXXX.no-ip.biz/Twilio/WebSite/smsresponse.aspx", 10343))            ' Requests the latest source            Dim Response As HTTP_Client.HTTP_Response = WebSession.Post("")            ' Did we get the expected response? (a "200 OK")            If Response.ResponseCode <> 200 Then                Throw New ApplicationException("Unexpected HTTP response code: " + Response.ResponseCode.ToString())            End If            ' Fetches a response header            Debug.Print("Current date according to www.netmftoolbox.com: " + Response.ResponseHeader("date"))            ' Gets the response as a string            Debug.Print(Response.ToString())        Catch ex As Exception            Debug.Print(ex.ToString)        End Try    End Sub


#18 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 27 May 2013 - 05:28 PM

Try removing the HTTP:// part of ur URL, simplesocket does a dns lookup to get the uri's ip address, the HTTP:// part of a URL is used to define protocol, as U are using the HTTP client the HTTP part of the URI is implicit Hope that helps Nak.

#19 marksmanaz

marksmanaz

    Member

  • Members
  • PipPip
  • 15 posts

Posted 27 May 2013 - 05:47 PM

I found the answer here:

http://forums.netdui...+iis#entry49398

 

You cannot prefix the address with "http://"

 

 

Thanks NakChak I didn't see your post until after I posted but thank you very much for the reply.

 

#20 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 27 May 2013 - 07:33 PM

No worries it catches me out all the time, so used to the httpclient object in .net 4.5 I keep putting HTTP I front of URLs, should probably write a netmf implementation which behaves the same.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.