WEB开发网
开发学院软件开发Java HttpClient4 Post XML到一个服务器上 阅读

HttpClient4 Post XML到一个服务器上

 2010-08-20 00:00:00 来源:WEB开发网   
核心提示: 服务端接收方式:packagecom;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax

服务端接收方式:

package com; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
/** 
* 接收XLM请求 
* 
* @author leizhimin 2010-7-8 1:02:42 
*/ 
public class GenXmlServlet extends HttpServlet { 
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
//                String xml = req.getParameter("xmldata"); 
                resp.setContentType("text/xml"); 
                resp.setCharacterEncoding("GBK"); 
                PrintWriter out = resp.getWriter(); 
//                out.println(xml); 
//                System.out.println(xml); 
                System.out.println("----------------------"); 
                InputStreamReader reader = new InputStreamReader(req.getInputStream(), "GBK"); 
                char[] buff = new char[1024]; 
                int length = 0; 
                while ((length = reader.read(buff)) != -1) { 
                        String x = new String(buff, 0, length); 
                        System.out.println(x); 
                        out.print(x); 
                } 
        } 
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
                resp.setContentType("text/html"); 
                PrintWriter out = resp.getWriter(); 
                out.println("<html>"); 
                out.println("<head>"); 
                out.println("<title>Hello World!</title>"); 
                out.println("</head>"); 
                out.println("<body>"); 
                out.println("<h1>Hello World!!</h1>"); 
                out.println("</body>"); 
                out.println("</html>"); 
        } 
}

web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
                 version="2.5"> 
        <servlet> 
                <servlet-name>GenXmlServlet</servlet-name> 
                <servlet-class>com.GenXmlServlet</servlet-class> 
        </servlet> 
        <servlet-mapping> 
                <servlet-name>GenXmlServlet</servlet-name> 
                <url-pattern>/GenXmlServlet</url-pattern> 
        </servlet-mapping> 
</web-app>

出处http://lavasoft.blog.51cto.com/62575/347157

上一页  1 2 3 

Tags:HttpClient Post XML

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接