Suppose we want to provide a small service that uses the HTTP protocol for communication, and the client post some data to the server. The client may not be a PC, but may not submit data in a web form format. It may be a desktop application running on a PC or a mobile device.
It is extremely simple for the server to receive such a request.CodeYou can achieve:
Create a new generic handler (*. ashx) on the web site. The Code is as follows:
[CSHARP]
-
- <% @ Webhandler Language = "C #" class = "Echo" %>
-
-
-
- Using system;
-
- Using system. Web;
-
- Using system. IO;
-
-
-
- Public class ECHO: ihttphandler
-
- {
-
- Private system. Text. Encoding defaultencoding = system. Text. encoding. utf8;
-
-
-
- Public void processrequest (httpcontext context)
-
- {
-
- Context. response. contenttype = "text/plain ";
- Context. response. contentencoding = defaultencoding;
-
-
-
- Stream inputstream = context. Request. inputstream;
-
- Using (streamreader reader = new streamreader (inputstream, defaultencoding ))
-
- {
-
- String requestcontent = reader. readtoend ();
-
- String responsecontent = string. Format ("Received: {0} <= end", requestcontent );
-
-
-
- Context. response. Write (responsecontent );
-
- }
-
- }
-
-
-
- Public bool isreusable
-
- {
-
- Get {return false ;}
-
- }
-
- }
<% @ Webhandler Language = "C #" class = "Echo" %> using system; using system. web; using system. io; public class ECHO: ihttphandler {private system. text. encoding defaultencoding = system. text. encoding. utf8; Public void processrequest (httpcontext context) {context. response. contenttype = "text/plain"; context. response. contentencoding = defaultencoding; stream inputstream = context. request. inputstream; using (streamreader reader = new streamreader (inputstream, defaultencoding) {string requestcontent = reader. readtoend (); string responsecontent = string. format ("Received: {0} <= end", requestcontent); context. response. write (responsecontent) ;}} public bool isreusable {get {return false ;}}}
Then we write a simple client to test it. In this client, we implement the httpclient class to perform the http post operation:
[CSHARP]
- //-----------------------------------------------------------------------
-
- // HTTP protocol client.
-
- //-----------------------------------------------------------------------
-
-
- Namespace consoleclient
-
- {
-
- Using system;
-
- Using system. IO;
-
- Using system. net;
-
- Using system. text;
-
-
- /// <Summary>
-
- /// HTTP protocol client.
-
- /// </Summary>
-
- Public class httpclient
-
- {
-
- /// <Summary>
-
- /// Post data to specific URL and get response content.
-
- /// </Summary>
-
- /// <Param name = "url"> the URL to post </param>
- /// <Param name = "postdata"> Post Data </param>
-
- /// <Returns> response content </returns>
-
- Public String post (string URL, string postdata)
-
- {
-
- Uri uri = new uri (URL );
-
- Httpwebrequest request = (httpwebrequest) webrequest. Create (URI );
-
- Request. method = "Post ";
-
- Request. contenttype = "application/X-WWW-form-urlencoded ";
-
-
- Encoding encoding = encoding. utf8;
-
- Byte [] bytes = encoding. getbytes (postdata );
-
-
- Request. contentlength = bytes. length;
-
-
- Using (Stream writer = request. getrequeststream ())
-
- {
-
- Writer. Write (bytes, 0, bytes. Length );
-
- Writer. Close ();
-
- }
-
- Return this. readresponse (request );;
-
- }
-
-
- /// <Summary>
-
- /// Read response content from http request result.
-
- /// </Summary>
-
- /// <Param name = "request"> HTTP request object </param>
-
- /// <Returns> response content. </returns>
-
- Private string readresponse (httpwebrequest request)
-
- {
-
- String result = string. empty;
-
-
- Using (httpwebresponse response = (httpwebresponse) request. getresponse ())
-
- {
-
- Using (Stream responsestream = response. getresponsestream ())
-
- {
-
- Using (streamreader reader = new streamreader (responsestream, encoding. utf8 ))
-
- {
- Result = reader. readtoend ();
-
- Reader. Close ();
-
- }
-
- }
-
- }
-
-
- Return result;
-
- }
-
- }
-
- }
//-----------------------------------------------------------------------
// HTTP protocol client. // ------------------------------------------------------------------------- namespace leleclient {using system; using system. io; using system. net; using system. text; // <summary> // HTTP protocol client. /// </Summary> public class httpclient {// <summary> // post data to specific URL and get response content. /// </Summary> /// <Param name = "url"> the URL to post </param> /// <Param name = "postdata"> Post Data </param> // <returns> response content </returns> Public String post (string URL, string postdata) {URI uri = new uri (URL); httpwebrequest request = (httpwebrequest) webrequest. create (URI); Request. method = "Post"; request. contenttype = "application/X-WWW-form-urlencoded"; encoding = encoding. utf8; byte [] bytes = encoding. getbytes (postdata); Request. contentlength = bytes. length; using (Stream writer = request. getrequeststream () {writer. write (bytes, 0, bytes. length); writer. close ();} return this. readresponse (request);} // <summary> // read response content from http request result. /// </Summary> /// <Param name = "request"> HTTP request object </param> /// <returns> response content. </returns> private string readresponse (httpwebrequest request) {string result = string. empty; using (httpwebresponse response = (httpwebresponse) request. getresponse () {using (Stream responsestream = response. getresponsestream () {using (streamreader reader = new streamreader (responsestream, encoding. utf8) {result = reader. readtoend (); reader. close () ;}} return result ;}}}
Then we can get the http post response:
[CSHARP]
- // Replace the string content with your actual address please.
- String defaulturl = "http: // SERVERNAME: 8081/webpath/anyservice. ashx ";
- Httpclient client = new httpclient ();
- String response = client. Post (defaulturl, @ "abc 123! Test @");
- Console. outputencoding = system. Text. encoding. utf8;
- Console. writeline (response );
// Replace the string content with your actual address please. string defaulturl = "http: // SERVERNAME: 8081/webpath/anyservice. ashx "; httpclient client = new httpclient (); string response = client. post (defaurl URL, @ "abc 123! Test @ "); console. outputencoding = system. Text. encoding. utf8; console. writeline (response );
assume that both the client and server agree to adopt a certain format for data serialization and deserialization. For example, both clients adopt JSON, and the client encapsulates the object through JSON and then post it to the server, the server then parses the object from the JSON string using JSON, which facilitates data transmission. If we want to compress the data volume, we can perform gzip compression and decompression. If we want to consider security, we can add security tokens in the object structure for authentication, and uses some encryption algorithm to encrypt and decrypt strings. How to use it depends on your specific application. It is easy to build a lightweight service of your own.