一、httpservice接口开发
httpservice是Java语言自带的HTTP服务器,提供了丰富的HTTP服务端构建方法。它可以对底层的字节流封装,对HTTP Header进行处理,提供访问参数的各种方式等。
httpservice提供了HttpService接口,我们可以通过实现这个接口来开发HTTP服务端。在接口中,我们只需要实现handle方法,并在该方法中编写处理HTTP请求的逻辑。
public interface HttpService { void handle( HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException; }
其中,HttpRequest是封装了HTTP头和HTTP实体的类,HttpResponse封装了响应头和实体的类,HttpContext用来存储HTTP上下文信息。
二、httpservice是什么
httpservice是Java类库中自带的HTTP服务器,可以提供监听HTTP请求,处理HTTP请求的功能。使用httpservice搭建HTTP服务端,既可以提供Web服务,也可以提供自定义的HTTP API接口。
httpservice使用简单,轻量级,具有良好的可扩展性和性能优势,可以处理同步和异步HTTP请求。
三、httpservice启动项
在使用httpservice之前,需要新建一个HttpServer对象。HttpServer对象是httpservice中最为重要、最为基础的启动项。
HttpServer httpServer = HttpServer.create(new InetSocketAddress(8080),0)
其中,create方法创建了一个默认的Server对象,同时绑定了一个监听地址和端口号。第一个参数表示监听端口号,第二个参数为可选参数,表示并发请求的队列大小。
创建HttpServer对象后,需要注册HttpHandler。HttpHandler是httpservice中对请求进行处理的关键。
httpServer.createContext("/",new MyHttpHandler())
上述代码片段中,将MyHttpHandler对象注册到监听”/”路径下,随后便可以对该路径下的请求进行处理。
四、httpservice实现接口调用
当接收到某个请求时,HttpHandler将根据请求的路径查找具体的处理逻辑,并调用待实现的HttpService接口的实现类的handle方法。
下面是一个简单的HttpService实现类,处理来自客户端的请求并返回相应的Response。
public class MyHttpService implements HttpService { @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH); if (!method.equals("GET") && !method.equals("HEAD")) { throw new MethodNotSupportedException(method + " method not supported"); } //检查URI是否请求了一个资源 String target = request.getRequestLine().getUri(); String queryString = ""; if(target.indexOf("?")>0){ queryString = target.substring(target.indexOf("?")+1); target = target.substring(0,target.indexOf("?")); } //设置响应头 response.setStatusCode(HttpStatus.SC_OK); response.setEntity(new StringEntity( "请求的资源:" +target+"
"+queryString+"
", ContentType.create("text/html", "UTF-8"))); } }
五、httpservice怎么打开
httpservice的启动方式比较灵活,可以在命令行或者脚本中启动,也可以将它打包成一个可执行JAR文件。
下面是通过命令行方式启动httpservice的方法:
C:\>java -cp "d:\myjar.jar" com.sun.net.httpserver.MyHttpHandler
其中,-cp参数表示类路径,即jar文件路径,com.sun.net.httpserver.MyHttpHandler是我们实现的HttpHandler类名。
六、httpservicerequest
HttpRequest对象就是来自客户端的请求。通过HttpRequest对象,我们可以获取请求的方法、路径、参数等信息,也可以获取请求头或者实体内容。
下面是HttpRequest对象中常用的方法:
HttpRequest request = ... ; request.getRequestLine().getMethod(); //获取请求方法 request.getRequestLine().getUri(); //获取请求路径 request.getRequestLine().getProtocolVersion(); //获取请求协议版本 request.getHeaders("Content-Type"); //获取指定请求头 request.getEntity(); //获取请求实体
七、httpservice如何启动
接下来,我们来看看通过Java代码启动httpservice的方式。方法如下:
public static void main(String[] args) throws IOException { int port = 8080; HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); server.createContext("/test", new MyHttpHandler()); server.createContext("/test1", new MyHttpHandler()); server.setExecutor(null); // creates a default executor server.start(); System.out.println("Server is listening on port " + port); }
当启动httpservice后,它会监听来自客户端的请求。HttpHandler将解析客户端的请求,并调用对应路径下的HttpService实现类处理请求。
八、httpservice实例
下面的例子中,我们自定义了一个Hello World HttpService,它接收来自客户端的请求,并以HTML格式返回“Hello World!”。
public class MyHttpService implements HttpService { public static void main(String[] args) throws IOException { HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); server.createContext("/hello", new MyHttpHandler()); server.start(); System.out.println("Server started."); } @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { String query = request.getRequestLine().getUri().getQuery(); String name = query != null ? query : "World"; response.setEntity(new StringEntity( "<html><body><h1>Hello, "+name+"!</h1></body></html>", ContentType.create("text/html", "UTF-8"))); } }
九、httpservice错误返回值选取
在httpservice中,当捕获到异常后,一般会返回400、401、404、405等HTTP状态码。
下表列举了httpservice中一些常用的HTTP错误码以及相应的解释:
错误码 | 含义 |
---|---|
400 | 请求参数错误 |
401 | 未经授权,拒绝访问 |
404 | 请求的资源不存在 |
405 | 请求方法不允许 |
500 | 服务器内部错误 |
原创文章,作者:XALS,如若转载,请注明出处:https://www.506064.com/n/145525.html