码迷,mamicode.com
首页 > Web开发 > 详细

HttpServerProvider实现http服务接口

时间:2016-10-26 19:44:34      阅读:907      评论:0      收藏:0      [点我收藏+]

标签:except   信息   erp   parameter   exception   控制台   ram   main   add   

啥也不说了,直接上代码,简单的示例。

 

服务端代码:

package dyan.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.spi.HttpServerProvider;


public class DealMain
{
    
    public static void main(String[] args) throws Exception
    {
        HttpServerProvider provider = HttpServerProvider.provider();
        
        HttpServer server = provider.createHttpServer(new InetSocketAddress(6688), 10);
        
        server.createContext("/dyan", new DealMain().new DyanHttpHandler());
        
        server.setExecutor(null);
        server.start();
        
    }
    
    
    class DyanHttpHandler implements HttpHandler
    {

        @Override
        public void handle(HttpExchange arg0) throws IOException
        {
            System.out.println("accept an exchange from internet.....");
            
            String resp = "your request message i get it!";
            
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(arg0.getRequestBody()));
            
            System.out.println(bufferedReader.readLine());
            
            arg0.sendResponseHeaders(200, resp.getBytes().length);
            
            OutputStream out = arg0.getResponseBody();
            out.write(resp.getBytes());
            
            out.flush();
            arg0.close();
        }
        
    }
    
}

 

 

请求代码:

 public static void main(String[] args) throws Exception
    {
        URL url = null;
        HttpURLConnection urlConnection = null;
        
        url = new URL("http://localhost:6688/dyan");
        urlConnection = (HttpURLConnection)url.openConnection();
        
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setUseCaches(false);
        
        System.out.println("begin send parameters....");
        
        urlConnection.getOutputStream().write("i am dyan".getBytes());
        urlConnection.getOutputStream().flush();
        urlConnection.getOutputStream().close();
        
        InputStream in = urlConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
        
        System.out.println(bufferedReader.readLine());
        
        bufferedReader.close();
        
        
        urlConnection.disconnect();
    }

 

先执行服务端代码,将服务端启动起来,来接收请求。

再执行请求代码,发起请求。

控制台打印信息如下:

  1、请求端

begin send parameters....
your request message i get it!

 

  2、服务端

accept an exchange from internet.....
i am dyan

HttpServerProvider实现http服务接口

标签:except   信息   erp   parameter   exception   控制台   ram   main   add   

原文地址:http://www.cnblogs.com/ahudyan-forever/p/6001259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!