HttpServer extends NanoHTTPD

import java.util.*;
import java.io.IOException;
import java.util.logging.Logger;

import org.nanohttpd.protocols.http.IHTTPSession;
import org.nanohttpd.protocols.http.NanoHTTPD;
import org.nanohttpd.protocols.http.request.Method;
import org.nanohttpd.protocols.http.response.Response;
import org.nanohttpd.protocols.http.response.IStatus;
import org.nanohttpd.util.ServerRunner;

public class HttpServer extends NanoHTTPD {
    /*这类就是要自定义一些返回值,我在这里定义了700。都属于自定义*/
    public enum Status implements IStatus {
        SWITCH_PROTOCOL(101, "Switching Protocols"),
        NOT_USE_POST(700, "not use post");

        private final int requestStatus;
        private final String description;

        Status(int requestStatus, String description) {
            this.requestStatus = requestStatus;
            this.description = description;
        }

        @Override
        public String getDescription() {
            return null;
        }

        @Override
        public int getRequestStatus() {
            return 0;
        }
    }

    public HttpServer(int port) {
        super(port);
    }

    public HttpServer() {
        super(8080);
    }

    @Override
    public Response serve(IHTTPSession session) {
        /*我在这里做了一个限制,只接受POST请求。这个是项目需求。*/
        if (Method.POST.equals(session.getMethod())) {
            Map<String, String> files = new HashMap<String, String>();
            /*获取header信息,NanoHttp的header不仅仅是HTTP的header,还包括其他信息。*/
            Map<String, String> header = session.getHeaders();

            try {
                /*这句尤为重要就是将将body的数据写入files中,大家可以看看parseBody具体实现,倒现在我也不明白为啥这样写。*/
                session.parseBody(files);
                /*看就是这里,POST请教的body数据可以完整读出*/
                String body = session.getQueryParameterString();
                CNTrace.d("header : " + header);
                CNTrace.d("body : " + body);
                /*这里是从header里面获取客户端的IP地址。NanoHttpd的header包含的东西不止是HTTP heaer的内容*/                
                CNTrace.d("http-client-ip : " + header.get("http-client-ip"));
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ResponseException e) {
                e.printStackTrace();
            }
            /*这里就是为客户端返回的信息了。我这里返回了一个200和一个HelloWorld*/
            return Response.newFixedLengthResponse(org.nanohttpd.protocols.http.response.Status.OK, "text/html", "HelloWorld");
        }else
            return Response.newFixedLengthResponse(Status.NOT_USE_POST, "text/html", "use post");
    }
}
class CNTrace
{
    public static void d(String arg) 
    {
        System.out.println(arg);
    }
}
import java.io.IOException;
import org.nanohttpd.util.ServerRunner;

public class MyTry 
{
    private HttpServer mHttpServer = null;//这个是HttpServer的句柄。

    public static void main(String[] args) 
    {
        LocalIPAddress testObj=new LocalIPAddress();
        System.out.println("LocalHostIp : " + testObj.getLocalHostIp());    
        new MyTry().onCreate();
        //
    }

      public void onCreate() {

        /*  ServerRunner.run(HttpServer.class);*/
        //在这里开启HTTP Server。
        mHttpServer = new HttpServer(8092);
        try {
            mHttpServer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Server started, Hit Enter to stop.\n");
        try {
            System.in.read();
        } catch (Throwable ignored) {
        }
        mHttpServer.stop();
        System.out.println("Server stopped.\n");        
    }

}


 

  

 

posted @ 2017-10-25 13:09  sky20080101  阅读(403)  评论(0)    收藏  举报