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

WebRequest takes a long time


  • Please log in to reply
8 replies to this topic

#1 JoRu1407

JoRu1407

    Advanced Member

  • Members
  • PipPipPip
  • 37 posts

Posted 04 October 2012 - 10:42 AM

Hello guys,

I've programmed a C# program for my Netduino Plus that turns around a PTZ camera with an WebRequest - But there's one problem:
The WebRequest takes a very longt time - I think about 3 seconds. When I do the same WebRequest from an C# program on Windows the WebRequest takes less than 0.5 seconds.

Here's my code:
public static void InStateToServer(int Portnummer)
{
  WebRequest WebReq = WebRequest.Create("http://192.168.2.231/decoder_control.cgi?command=31&user=admin&pwd=abc");
  
  try
  {
    WebResponse WebResp = WebReq.GetResponse();
  }
  catch
  { 
  }
}

EDIT

I forgot to say that it's the same problem by calling my home server to submit a new instate with the following request "http://192.168.2.200...nstate/1/true".

END EDIT

Does anyone have an idea to solve this problem?
Many thanks in advance.

JoRu1407

#2 Nicky

Nicky

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationDenmark

Posted 04 October 2012 - 02:26 PM

Try doing this instead

public static void InStateToServer(int port)
{
   using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
   {
      socket.Connect("192.168.2.231", 80);

      using (NetworkStream ns = new NetworkStream(socket))
      {
         byte[] bytes = System.Text.Encoding.UTF8.GetBytes("GET /decoder_control.cgi?command=" + port + "&user=admin&pwd=abc HTTP/1.1\r\n");
         ns.Write(bytes, 0, bytes.Length);
      }
   }
}

It's way more lightweight, and as a bonus you can get rid of the System.Http reference :)

Note: I didn't have my Netduinos here, so I've written the code in a Console application, but I don't think there is any difference...

edit: forgot "GET" + "HTTP/1.1"

ntools
TCP Listener (Beta) · FTP Server (Alpha)
Netduino Plus Go Module · Xml Parser
http://ntools.codeplex.com/


#3 JoRu1407

JoRu1407

    Advanced Member

  • Members
  • PipPipPip
  • 37 posts

Posted 04 October 2012 - 06:44 PM

Thank you for your answer, Nicky. I'm going to try that until tomorrow and will inform you, if it works. Many greetings, JoRu1407

#4 JoRu1407

JoRu1407

    Advanced Member

  • Members
  • PipPipPip
  • 37 posts

Posted 07 October 2012 - 09:29 AM

Hello Nicky, sorry, but I had no time yet to test your code. But now, I've a problem: Regarding the line "socket.Connect("192.168.2.231", 80);" Visual Studio outlines the following error: No overload for the Connect method takes two arguments. Keine Überladung für die Connect-Methode nimmt 2 Argumente an. I've no idea how to solve this. Could you please help me again? :)

#5 Kem

Kem

    Member

  • Members
  • PipPip
  • 17 posts

Posted 07 October 2012 - 10:17 AM

As Nicky mentioned, he did not have the Netduino at his hands, so he used the normal .Net framework. The Connect method on a Socket has different overloads for NETMF.

socket.Connect(new IPEndPoint(IPAddress.Parse("192.168.2.231"), 80));

O, don't forget to import the following namespace, cause the IPEndPoint and IPAddress live there :)

using System.Net;

Cheers,

Kem

#6 Nicky

Nicky

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationDenmark

Posted 07 October 2012 - 10:18 AM

Ah sorry... told you that I wrote it to a desktop environment ;) Try something like this instead... I still didn't have chance to test on a device, or see my own code...

Try:

public static void InStateToServer(int port)
{
   IPHostEntry host = Dns.GetHostEntry("192.168.2.231");
   IPEndPoint endPoint = new IPEndPoint(host.AddressList[0], 80);

   using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
   {
      socket.Connect(endPoint);

      using (NetworkStream ns = new NetworkStream(socket))
      {
         byte[] bytes = System.Text.Encoding.UTF8.GetBytes("GET /decoder_control.cgi?command=" + port + "&user=admin&pwd=abc HTTP/1.1\r\n");
         ns.Write(bytes, 0, bytes.Length);
      }
   }
}

ntools
TCP Listener (Beta) · FTP Server (Alpha)
Netduino Plus Go Module · Xml Parser
http://ntools.codeplex.com/


#7 Kem

Kem

    Member

  • Members
  • PipPip
  • 17 posts

Posted 07 October 2012 - 10:31 AM

Lol, that's another way of doing it, sorry for barging in like this :unsure:

#8 JoRu1407

JoRu1407

    Advanced Member

  • Members
  • PipPipPip
  • 37 posts

Posted 07 October 2012 - 02:14 PM

Thank you for your answers. That works great!

#9 Nicky

Nicky

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationDenmark

Posted 08 October 2012 - 07:13 AM

JoRu1407: Great. I'm glad I was able to help you (with a little help from Kem :P) Kem: Why sorry? I didn't post my reply, as an objection to yours. I simply didn't see your post, write my reply and press submit in about one minute... we posted at the sametime. Barging in... you came with a valid, well argumentet and well formulated input. Come here so I can pet you instead! ;)

ntools
TCP Listener (Beta) · FTP Server (Alpha)
Netduino Plus Go Module · Xml Parser
http://ntools.codeplex.com/





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.