关于Http Post Xml
来源:互联网 发布:微信支付php源码 编辑:程序博客网 时间:2024/06/07 02:16
最近遇到个项目,登录鉴权部分。
HTTP 消息头的请求行示例:
POST/mms/LoginAuth HTTP/1.1:
- <Login_reqReq>
- <userName>用户名</userName>
- <pass>密码</pass>
- </Login_reqReq>
<Login_reqReq> <userName>用户名</userName> <pass>密码</pass></Login_reqReq>
响应的是xml格式的数据信息。
一开始还真没点头绪,不知道这个xml咋请求的,是不是也写成a.do?userName=用户名&pass=111。
在网上搜了下资料,自己还真是有点见识浅薄呀~~这个都不知道
一、概述
在不同的应用之间传递数据,可以通过web service的方法,同时还可以通过Http Post Xml的方法,相比而言,通过web service传递数据灵活,但是配置起来较为麻烦,涉及到新知识的学习,而通过Http Post Xml传递数据,不需要涉及新的知识,但是灵活性稍差,需要客户端和服务端事先约定好xml数据的结构。
Http Post Xml方式传递数据在跟移动、联通等电信运营商之间合作时,经常会用到,一般涉及到下面的知识点:
Ø Java网络编程(java.net包)
Ø Java IO编程(java.io包)
Ø 文档对象模型(DOM)
Ø Java解析xml(javax.xml.parsers包)
二、请求实现并处理返回结束(公共模块类)
通过Http Post Xml传递数据,客户端一般是通过URL建立到服务端的连接,向服务端发送xml数据,然后获取服务端的响应并进行解析:
自己写了个公共类BaseServletRequest.java 免得各个地方使用的时候还要又写一次,代码如下
- public class BaseServletRequest
- {
- public static Document doTheProcess(String xmlString, String urlStr)
- {
- DataInputStream input = null;
- java.io.ByteArrayOutputStream out = null;
- try
- {
- byte[] xmlData = xmlString.getBytes();
- // 获得到位置服务的链接
- URL url = new URL(urlStr);
- URLConnection urlCon = url.openConnection();
- urlCon.setDoOutput(true);
- urlCon.setDoInput(true);
- urlCon.setUseCaches(false);
- // 将xml数据发送到位置服务
- urlCon.setRequestProperty("Content-Type", "text/xml");
- urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length));
- DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream());
- printout.write(xmlData);
- printout.flush();
- printout.close();
- input = new DataInputStream(urlCon.getInputStream());
- byte[] rResult;
- out = new java.io.ByteArrayOutputStream();
- byte[] bufferByte = new byte[256];
- int l = -1;
- int downloadSize = 0;
- while ((l = input.read(bufferByte)) > -1)
- {
- downloadSize += l;
- out.write(bufferByte, 0, l);
- out.flush();
- }
- rResult = out.toByteArray();
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document d = db.parse(new ByteArrayInputStream(rResult));
- return d;
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- out.close();
- input.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- return null;
- }
- }
public class BaseServletRequest{public static Document doTheProcess(String xmlString, String urlStr){DataInputStream input = null;java.io.ByteArrayOutputStream out = null;try{byte[] xmlData = xmlString.getBytes();// 获得到位置服务的链接URL url = new URL(urlStr);URLConnection urlCon = url.openConnection();urlCon.setDoOutput(true);urlCon.setDoInput(true);urlCon.setUseCaches(false);// 将xml数据发送到位置服务urlCon.setRequestProperty("Content-Type", "text/xml");urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length));DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream());printout.write(xmlData);printout.flush();printout.close();input = new DataInputStream(urlCon.getInputStream());byte[] rResult;out = new java.io.ByteArrayOutputStream();byte[] bufferByte = new byte[256];int l = -1;int downloadSize = 0;while ((l = input.read(bufferByte)) > -1){downloadSize += l;out.write(bufferByte, 0, l);out.flush();}rResult = out.toByteArray();DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();DocumentBuilder db = dbf.newDocumentBuilder();Document d = db.parse(new ByteArrayInputStream(rResult));return d;}catch (Exception e){e.printStackTrace();}finally{try{out.close();input.close();}catch (Exception ex){ex.printStackTrace();}}return null;}}
三、客户端的实现,调用了刚才公共类的处理 ClientDemo.java
- public class ClientDemo
- {
- public static void main(String[] args)
- {
- String xmlString = "<?xml version='1.0' encoding='gb2312'?><Req><EventContentReq>"
- + "<EventID>101</EventID></EventContentReq></Req>";
- String urlStr = "http://localhost:8080/Foster_Blog/HttpRequestDemo";
- // 调用参数传递,返回一个document,要什么值再直接从中去取。
- Document d = BaseServletRequest.doTheProcess(xmlString, urlStr);
- String TaskAddr = d.getElementsByTagName("TaskAddr").item(0).getFirstChild().getNodeValue();
- System.out.println("TaskAddr:" + TaskAddr);
- }
- }
public class ClientDemo{public static void main(String[] args){String xmlString = "<?xml version='1.0' encoding='gb2312'?><Req><EventContentReq>"+ "<EventID>101</EventID></EventContentReq></Req>";String urlStr = "http://localhost:8080/Foster_Blog/HttpRequestDemo"; // 调用参数传递,返回一个document,要什么值再直接从中去取。Document d = BaseServletRequest.doTheProcess(xmlString, urlStr);String TaskAddr = d.getElementsByTagName("TaskAddr").item(0).getFirstChild().getNodeValue();System.out.println("TaskAddr:" + TaskAddr);}}
四、服务器端代码
- protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,
- IOException
- {
- try{
- //解析对方发来的xml数据,获得EventID节点的值
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document d = db.parse(request.getInputStream());
- // 请求时传递的哪些值,需要的就取出来。。
- String evtid = d.getElementsByTagName("EventID").item(0).getFirstChild().getNodeValue();
- System.out.println("evtid" + evtid);
- //根据evtid查找任务,生成xml字符串(格式要正确。。)
- String xmlString = "<Req>" + "<EventContentReq>"
- + "<TaskAddr> U should study.....</TaskAddr >" + "</EventContentReq>" + "</Req>";
- System.out.println("returned xmlString:" + xmlString);
- //把xml字符串写入响应
- byte[] xmlData = xmlString.getBytes();
- response.setContentType("text/xml");
- response.setContentLength(xmlData.length);
- ServletOutputStream os = response.getOutputStream();
- os.write(xmlData);
- os.flush();
- os.close();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ try{ //解析对方发来的xml数据,获得EventID节点的值 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document d = db.parse(request.getInputStream()); // 请求时传递的哪些值,需要的就取出来。。 String evtid = d.getElementsByTagName("EventID").item(0).getFirstChild().getNodeValue(); System.out.println("evtid" + evtid); //根据evtid查找任务,生成xml字符串(格式要正确。。) String xmlString = "<Req>" + "<EventContentReq>"+ "<TaskAddr> U should study.....</TaskAddr >" + "</EventContentReq>" + "</Req>"; System.out.println("returned xmlString:" + xmlString); //把xml字符串写入响应 byte[] xmlData = xmlString.getBytes(); response.setContentType("text/xml"); response.setContentLength(xmlData.length); ServletOutputStream os = response.getOutputStream(); os.write(xmlData); os.flush(); os.close(); } catch(Exception e){ e.printStackTrace(); } }
至此,功能结束。 总结一下请求有以下几种方式: ajax直接请求,或者servlet, action等, 其实都是等效的。还有web Service,这个还要弄客户端,服务端等比较麻烦点。 再一个呢 就是现在的Http Post Xml数据请求了。 My pleasure to share this article for all.
- 关于Http Post Xml
- 关于Http Post XML
- 关于Http Post Xml
- 关于Http Post Xml
- http post xml
- http+post+xml
- .net http post xml 例子
- Http post 请求发送 xml
- 关于http 协议中POST
- 关于http status 405 post
- http 关于get和post
- 关于通过JAVA的HTTP POST请求传输XML型数据
- 关于通过JAVA的HTTP POST请求传输XML型数据
- 关于安全的建议:对投入使用的 XML Web Services 禁用 HTTP-GET 和 HTTP-POST 协议
- 关于安全的建议:对投入使用的 XML Web Services 禁用 HTTP-GET 和 HTTP-POST 协议
- 关于安全的建议:对投入使用的 XML Web Services 禁用 HTTP-GET 和 HTTP-POST 协议
- C# to POST HTTP with XML
- Http Post Xml形式传递数据
- matlab python c的循环比较
- REDIS字典(dict)
- 背包问题源码
- 关于计算机研究和写作的链接收集
- PG常用的几个查询(1)
- 关于Http Post Xml
- postgresql streaming replication
- linux用户和组帐户管理
- C++程序怎样调用exe可执行文件
- 第9周项目一-Complex类
- postgresql pg_buffercache
- 手机尾号评分
- unity3d摄影机晃动特效【脚本】
- Win下rjb安装