首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

简略的Httpserver,Httpclient,xml

2012-08-24 
简单的Httpserver,Httpclient,xml简单演示Http SERVER,CLIENT发送接收xml的流程。XMLSERVER:监听某端口:接

简单的Httpserver,Httpclient,xml
简单演示Http SERVER,CLIENT发送接收xml的流程。

XMLSERVER:

监听某端口:接收xml,并解析。

    public class XmlServer { 
     
        private int port; 
         
        public XmlServer() { 
        } 
         
        public XmlServer(int port) { 
            this.port = port; 
        } 
         
        public void start(){ 
             
            try { 
                HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); 
             
                server.createContext("/",new HttpHandler() { 
                     
                    @Override 
                    public void handle(HttpExchange he) throws IOException { 
     
                        try { 
                            String ip = he.getRemoteAddress().getAddress().getHostAddress(); 
                             
                            System.out.println(ip); 
                             
                            InputStream input_stream = he.getRequestBody();  
                             
    //                        BufferedReader reader = new BufferedReader(new InputStreamReader(input_stream)); 
    //                        StringBuilder str_builder = new StringBuilder(); 
    // 
    //                        String line = null;    
    //                         
    //                        while ((line = reader.readLine()) != null)  
    //                        {     
    //                            str_builder.append(line + "\n");     
    //                        }     
    //                         
    //                        System.out.println("接收:"+str_builder); 
                             
                            SAXReader saxReader = new SAXReader(); 
                             
                            Document  document = saxReader.read(new InputStreamReader(input_stream)); 
     
                            Element rootElm = document.getRootElement(); 
                             
                            String username = rootElm.element("username").getText(); 
                             
                            String password = rootElm.element("password").getText(); 
                             
                            System.out.println(username); 
                            System.out.println(password); 
                            //登录, 
                            String ret = "OK"; 
                            he.sendResponseHeaders(200, ret.getBytes("utf-8").length); 
                             
                            OutputStream os = he.getResponseBody(); 
                             
                            os.write(ret.getBytes()); 
                             
                            os.close(); 
                        } catch (Exception e) { 
                            e.printStackTrace(); 
                        } 
                         
                    } 
                }); 
                 
                server.start(); 
                 
                System.out.println("开始监听"+port+"端口"); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
             
        } 
         
        public static void main(String[] args) { 
             
            new XmlServer(9988).start(); 
        } 
    } 

XmlClient:

发送请求


[java] view plaincopy

    public class XmlClient { 
         
        public static void main(String[] args) { 
             
             
            try { 
                 
                String xml = "<?xml version='1.0' encoding='utf-8'?>" + 
                        "<message>" + 
                        "<username>ayli</username>" + 
                        "<password>liya</password>" + 
                        "</message>"; 
                 
                HttpClient client = new DefaultHttpClient(); 
                 
                HttpPost post = new HttpPost("http://localhost:9988"); 
                 
                StringEntity entity = new StringEntity(xml, "text/html", "utf-8"); 
                 
                post.setEntity(entity); 
                 
                ResponseHandler<String> response = new BasicResponseHandler(); 
                 
                String ret = client.execute(post, response); 
                 
                System.out.println(ret); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            }  
             
             
        } 
     
    } 


注:需要dom4j-1.6,httpclient-4.1相关的jar包

热点排行