[. Net MF network development board research-02] HTTP server function demonstration

Source: Internet
Author: User

The Web server introduced in the previous blog is actually a socket programming application. This article describes the real HTTP server and supports get and post functions.

We also modify the official example HTTP server. To make the example clearer, we try to make the code simpler.

Directly modify the main program to the following code:

Public static void main ()

{

Try

{

Runserver ("HTTP ");

}

Catch (exception E)

{

Debug. Print (E. Message );

}

}

The core code is the support for get and post requests. Do not change it here. The Code is as follows:

Internal static void runserver (string prefix)

{

Httplistener listener = new httplistener (prefix,-1 );

Listener. Start ();

While (true)

{

Httplistenerresponse response = NULL;

Httplistenercontext context = NULL;

Try

{

Context = listener. getcontext ();

Response = context. response;

Httplistenerrequest request = context. request;

Switch (request. httpmethod. toupper ())

{

Case "get": getrequest (context); break;

Case "Post": postrequest (context); break;

}

If (response! = NULL)

{

Response. Close ();

}

}

Catch

{

If (context! = NULL)

{

Context. Close ();

}

}

}

}

The GET request processing code is as follows. We have greatly simplified and adjusted it, and added an upload. asp processing module. The Code is as follows:

Private Static void getrequest (httplistenercontext context)

{

Httplistenerrequest request = context. request;

Httplistenerresponse response = context. response;

String strfilepath = getpathfromurl (request. rawurl );

Debug. Print (strfilepath );

Response. statuscode = (INT) httpstatuscode. OK;

// Start HTML document

String strresp = "<HTML> <body>. Net micro framework example HTTP server <p> ";

// Print requested verb, URL and version... adds information from the request.

Strresp + = "HTTP Method:" + request. httpmethod + "<br> requested URL: \" "+ request. rawurl +

"\" <Br> HTTP Version: "+ request. protocolversion +" \ "<p> ";

 

If (strfilepath. tolower () = "\ upload. asp ")

{

Strresp + = resource1.getstring (resource1.stringresources. postform );

}

Else

{

Strresp + = "file to access" + strfilepath + "<p> ";

Strresp + = "Directory: \" "+ strfilepath +" \ "does not exists ";

}

// Closes html

Strresp + = "</body>

// Sends it.

Byte [] messagebody = encoding. utf8.getbytes (strresp );

Response. contenttype = "text/html ";

Response. outputstream. Write (messagebody, 0, messagebody. Length );

}

The post processing section is simplified, but it does not change much. The related code is as follows:

Private Static void postrequest (httplistenercontext context)

{

// Retrieves request and response.

Httplistenerrequest request = context. request;

Httplistenerresponse response = context. response;

 

// Allocates buffer for reading of Message Body

Byte [] postdata = new byte [buffer_size];

 

// Now reads the posted data. The content length shocould be supplied.

// It is error not to have content length with POST request.

If (request. contentlength64> 0)

{

Debug. Print ("request headers :");

Debug. Print (request. headers. tostring ());

 

Long totalbytesreceived = 0;

Long contlen = request. contentlength64;

While (totalbytesreceived <contlen)

{

Int bytestoread = (INT) (contlen-totalbytesreceived );

// Limit to buffer size

Bytestoread = bytestoread <buffer_size? Bytestoread: buffer_size;

 

Int dataread = request. inputstream. Read (postdata, 0, bytestoread );

If (dataread = 0)

{

// Definitely some error. Means file incomplete.

Break;

}

Totalbytesreceived + = dataread;

};

// Sends response:

String strresp = "<HTML> <body>. Net micro framework example HTTP server <p> ";

// Print requested verb, URL and version... adds information from the request.

Strresp + = "HTTP Method:" + request. httpmethod + "<br> requested URL: \" "+ request. rawurl +

"\" <Br> HTTP Version: "+ request. protocolversion +" \ "<p> ";

Strresp + = "amount of data received ed in message body:" + totalbytesreceived + "<br> ";

Strresp + = "data of message body is discarded (if there is no filesystem). Please review HTTP Server Sample Code to add processing of data ";

Strresp + = "</body>

Response. statuscode = (INT) httpstatuscode. OK;

Byte [] messagebody = encoding. utf8.getbytes (strresp );

Response. contenttype = "text/html ";

Response. outputstream. Write (messagebody, 0, messagebody. Length );

}

Else // content length is missing, send Error Back

{

// Sends response:

String strresp = "<HTML> <body> content length is missing in POST request </body>

Byte [] messagebody = encoding. utf8.getbytes (strresp );

Response. contenttype = "text/html ";

Response. outputstream. Write (messagebody, 0, messagebody. Length );

}

}

After the program is compiled, it is directly deployed on the Development Board for running. Connect to the PC, open the IE browser, and enter http: // 192.168.0.100/a.txt. The effect is as follows:

It is intended that if there is a.txt file, the.txt content will be downloaded, but we have not processed it in the code.

The following shows how to use post. In IE, enter http: // 192.168.0.100/upload. asp.

IE displays the following content:

Select a file and click send file data to server. The HTTP server processes the POST request and returns the result. A new page is displayed for IE, for example:

In the post processing program of the HTTP server, we can obtain the content of the uploaded file. The content is not displayed here, but the size of the file is displayed, such as 611 bytes.

. Net micro framework also supports many network functions, such as the support for WCF, which need to be studied in detail in the future.

Bytes -----------------------------------------------------------------------------------------------------------------------

Source code/Documentation: http://www.sky-walker.com.cn/MFRelease/Sample/YFHttpServer.rar

Mf Forum: http://space.cnblogs.com/group/MFSoft/

Mf Development Board: http://item.taobao.com/item.htm? Id = 7117999726

Network Board: http://item.taobao.com/item.htm? Id = 10919470266

QQ group: 127465602 (full) 146524112

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.