Simple Java_web server processing POST request get content

Source: Internet
Author: User

 Packagecom.zhao.test;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.io.UnsupportedEncodingException;ImportJava.net.ServerSocket;ImportJava.net.Socket;ImportJava.net.URLDecoder;/** *  * @author *  */ Public classSample_iis { Public Static voidMain (string[] args) {StartServer (9000); }    Private Static voidStartServer (intPort) {System.out.println ("======>startserver"); Try{serversocket server=NewServerSocket (port); Server.setsotimeout (0); Socket Socket=NULL;  while(socket = server.accept ())! =NULL) {socket.setsotimeout (2000); byte[] buffer =New byte[2048]; InputStream is=Socket.getinputstream (); intLen =is.read (buffer);                GetContent (buffer, Len); ResponseText (socket,"{\" cmd\ ": \" response\ ", \" inof\ ": \" Success\ "}");            Socket.close (); }        } Catch(IOException e) {e.printstacktrace (); } System.out.println ("=====>"); }    /**     *      * @paramPsocket *@paramcontent*/    Private Static voidresponsetext (Socket psocket, String content) {StringBuffer buffer=NewStringBuffer (); //buffer.append ("http/1.1 ok\r\n"); //buffer.append ("Date:tue, Sep 1999 02:19:57 gmt\r\n"); //buffer.append ("server:apache/1.2.6\r\n"); //buffer.append ("connection:close\r\n"); //buffer.append ("content-type:text/html\r\n"); //buffer.append ("\ r \ n");buffer.append (content); Try{outputstream OS=Psocket.getoutputstream ();            Os.write (Buffer.tostring (). GetBytes ());            Os.flush ();        Os.close (); } Catch(IOException e) {e.printstacktrace (); }    }    /*** Get content * *@paramData *@paramLen *@return     */    Private StaticString GetContent (byte[] Data,intLen) {String result= ""; String content=NewString (data, 0, Len); System.out.println ("Content:" +content); String[] Lines= Content.split ("\ r \ n"); String Firststr= Lines[0]; //determine if it is a post or a getString method = Firststr.split ("") [0].tolowercase (); System.out.println ("Method:" +method); if("Get". Equals (method)) {            //Get}Else {            //PostString line = lines[lines.length-1]; intindex = line.indexof ("="); Result= line.substring (index + 1); Try{result= Urldecoder.decode (Result, "GBK"); System.out.println ("Result:" +result); } Catch(unsupportedencodingexception e) {e.printstacktrace (); }        }        returnresult; }}

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.