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

How to read data html with web server?

web server html

  • Please log in to reply
8 replies to this topic

#1 Archimede

Archimede

    Member

  • Validating
  • PipPip
  • 15 posts

Posted 30 July 2013 - 09:46 AM

hi everyone, :) I have a web server that receives data from the browser. The web server sends html code to the browser (html code is a edit text and a button) ... My goal is to read the text written by the user. when the web server receives the data from the browser receives this request that is:

GET / localhost? User = foo HTTP/1.1 Accept: text / html, application / xhtml + xml, * / * ....... .....

In this example the user has written foo which is located in the user "variable" ... How can I filter only the text written by the user? I have used IndexOf() but it doesn't work fine... :wacko:



#2 Dave1374

Dave1374

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts

Posted 30 July 2013 - 11:05 AM

hi everyone, :) I have a web server that receives data from the browser. The web server sends html code to the browser (html code is a edit text and a button) ... My goal is to read the text written by the user. when the web server receives the data from the browser receives this request that is:

GET / localhost? User = foo HTTP/1.1 Accept: text / html, application / xhtml + xml, * / * ....... .....

In this example the user has written foo which is located in the user "variable" ... How can I filter only the text written by the user? I have used IndexOf() but it doesn't work fine... :wacko:

 

Let's try with substring and indeof

 

string requestAnswer = what You already do to get the data string...

 

int startIndex = requestAnswer.IndexOf("User =");

int endIndex = requestAnswer.IndexOf("HTTP/1.1");

 

string data = requestAnswer.Substring(startIndex , endIndex );

 

hope that helps, i have to leave, ill edit my post later

 

 

http://www.dotnetperls.com/substring

http://www.dotnetperls.com/indexof



#3 Archimede

Archimede

    Member

  • Validating
  • PipPip
  • 15 posts

Posted 30 July 2013 - 10:17 PM

thank you Dave1374, i have tryed the substring and indexOf() but the variable data contains "user textuser http/1.1"....i have used again indexOf() on data and work fine...but when i try to write the text, the server web works fine but after 5-6 tests doesn't work....Why?

 

Help me please Dave...

 

i post the code that i have used:

 

class Server : IDisposable

{

private Socket socket = null;

 

public Server()

{

//Initialize Socket class

socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

//Request and bind to an IP from DHCP server

socket.Bind(new IPEndPoint(IPAddress.Any, 9050));

 

 

//Start listen for web requests

socket.Listen(10);

ListenForRequest();

}

 

public void ListenForRequest()

{

while (true)

{

using (Socket clientSocket = socket.Accept())

{

Thread.Sleep(10);

//Get clients IP

IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;

EndPoint clientEndPoint = clientSocket.RemoteEndPoint;

 

int bytesReceived = clientSocket.Available;

if (bytesReceived > 0)

{

//code html

string response3 = "<!DOCTYPE html>";

response3 += "<html>";

response3 +="<head>";

response3 +="<title>Netduino Plus</title>";

response3 +="</head>";

response3 +="<body>";

response3 +="<h1>Netduino Plus 2</h1>";

response3 += "<form name="+"input"+" action="+"localhost"+" method="+"get"+">"

+" Username: <input type="+"text"+" name="+"user"+">"

+"<input type=submit value=send>"+

"</form>";

response3 +="</body>";

response3 +="</html>";

 

clientSocket.Send(Encoding.UTF8.GetBytes(response3), response3.Length, SocketFlags.None);

 

//Get request

byte[] buffer = new byte[bytesReceived];

int byteCount = clientSocket.Receive(buffer);

//string request = new string(Encoding.UTF8.GetChars(buffer));

string request = Encoding.ASCII.GetString(buffer,0,byteCount);

 

int startIndex = request.IndexOf("user=");

int endIndex = request.IndexOf("HTTP/1.1");

string dati = request.Substring(startIndex,endIndex);

 

 

 

if (dati.IndexOf("archimede") != -1 )

{

Debug.Print("text contains your name");

 

 

}

else

{

if (dati.IndexOf("read") != -1 )

Debug.Print("user wants to read");

}

else

{

Debug.Print("text doesn't contain your name ");

Debug.Print(request);

Debug.Print(dati);

}

}

}

}

}

}

#region IDisposable Members

~Server()

{

Dispose();

}

public void Dispose()

{

if (socket != null)

socket.Close();

}

#endregion

}


					
					

#4 Dave1374

Dave1374

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts

Posted 31 July 2013 - 02:15 AM

but the variable data contains "user textuser http/1.1"....i have used again indexOf() on data and work fine...but when i try to write the text, the server web works fine but after 5-6 tests doesn't work....Why?

 

string request = Encoding.ASCII.GetString(buffer,0,byteCount);

 

int startIndex = request.IndexOf("user=");

int endIndex = request.IndexOf("HTTP/1.1");

string dati = request.Substring(startIndex,endIndex);

 

Hmmm

add a lower case... if it's still not working, it's because the indexof doesnt match exactly the way the data is.

 

string request = Encoding.ASCII.GetString(buffer,0,byteCount).ToLower();

 

int startIndex = request.IndexOf("user=");

int endIndex = request.IndexOf("http/1.1");

string dati = request.Substring(startIndex,endIndex);

 

also i dont see how the data contains "textuser" ...

 

btw I would change the name of the form

 

response3 += "<form name='netDuinoForm'  action='localhost' method='get'>"
+" Username: <input type='text' name='user'>"
+"<input type='submit' value='send'>"+
"</form>";
 
I dont have visual studio installed here so I can't test anything
 
also you might want to look into .Contains("wordToSearchInAString") , or you may want to look for regular expressions... but I think regex is a bit overkill.
 
if it doesn't work, send me the full exact data string,
 
Dave


#5 Archimede

Archimede

    Member

  • Validating
  • PipPip
  • 15 posts

Posted 31 July 2013 - 02:13 PM

Hi Dave,

i tried to run the server but now i have the error...(i don't know the because...) is argumentOutOfRangeException... i think that startIndex and endIndex haven't the correct contents... it is how if the browser send different request each time that communicates with the server...Yesterday i had no this error...

 

 

thanks for your help... you are very polite  :)

 

 

P.S. how can i post the code how have you made with my request in your response?(sorry for my english...) 



#6 Archimede

Archimede

    Member

  • Validating
  • PipPip
  • 15 posts

Posted 31 July 2013 - 02:46 PM

Hi Dave, i tried change the data in starIndex i have wrote "get" and the server now work fine..(mystery)...i have added also .ToLower() have made more tests and i haven't the errors... thank you very much... then in future if i will have errors i write in this post for your help... :)

New request : after this experience, i would like to read the radiobutton, for example the browser contains a default state the radio button and when the user change the state of the radio button the server receives this change... can you write an example code...?



#7 Graham.fry

Graham.fry

    Member

  • Members
  • PipPip
  • 15 posts

Posted 06 August 2013 - 11:44 AM

Hi

[font="calibri;"]In my request I have limited my response to 2 characters and my var indicator is light= this works every time[/font]

 
 //Get request
  byte[] buffer = new byte[bytesReceived];
  int byteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);
  string request = new string(Encoding.UTF8.GetChars(buffer));
  Debug.Print(request);

  //int countof = request.IndexOf("light="); //for my code
 int countof = request.IndexOf("user=");
  countof = countof + 6;
  //var s = request.Substring(countof, 2); //for my code

 

You may need to add the following and it may need some tweeking

 

 int noChatTotext = X //(this is the number of char to the end of user= it should constant)
 int lengthOfrequest = request.length(); //calculate the length of the hole request sent.
 int noCharToGet =  lengthOfrequest - noChatTotext; //the length of the text you want
 var s = request.Substring(countof, noCharToGet); //S will be the text you want



#8 NoxiaZ

NoxiaZ

    Member

  • Members
  • PipPip
  • 12 posts

Posted 06 August 2013 - 12:01 PM

Hmm.. think this way is alittle bit better, and you can use it to all the fields :)

string request = "GET / localhost? User = foo HTTP/1.1";Hashtable hashtable = new Hashtable();int indexQuestionMark = request.IndexOf("?");int httpLength = request.Length-request.LastIndexOf(" ") ;string data = request.Substring(indexQuestionMark +1,request.Length-indexQuestionMark-httpLength -1).Trim();string[] fields = data.Split('&');for(int i=0;i < fields.Length;i++){ string[] field = fields[i].Split('='); hashtable.Add(field[0].Trim(),field[1].Trim());}Console.WriteLine(hashtable["User"]);

Hope this help you a bit :)



#9 Archimede

Archimede

    Member

  • Validating
  • PipPip
  • 15 posts

Posted 21 August 2013 - 07:22 AM

thank you boys for your answers, i will prove to use these code lines...

 

thank you very much :)







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.