本实例是通过向服务器发送一个xml格式的数据
web端
servlet
package com.lin.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lin.utils.StreamTools;
public class XmlService extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
byte[] data=StreamTools.read(request.getInputStream());
String xml=new String(data,"UTF-8");
System.out.println("xml: "+xml);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
web.xml
<servlet>
<description></description>
<display-name>XmlService</display-name>
<servlet-name>XmlService</servlet-name>
<servlet-class>com.lin.servlet.XmlService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XmlService</servlet-name>
<url-pattern>/XmlService</url-pattern>
</servlet-mapping>
java访问
package com.lin.test;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.lin.utils.StreamTools;
public class XMLTest {
public static void main(String[] args) {
try {
new XMLTest().testSendXML();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void testSendXML() throws Exception{
InputStream stream=this.getClass().getResourceAsStream("video.xml");
byte data[]=StreamTools.read(stream);
String xml=new String(data);
String path="http://192.168.189.1:8080/videonews/XmlService";
HttpURLConnection conn=(HttpURLConnection) new URL(path).openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type","text/xml; charset=UTF-8");
conn.setRequestProperty("Content-length",String.valueOf(data.length));
conn.getOutputStream().write(data);
if(conn.getResponseCode()==200){
System.out.println("发送成功");
}else{
System.out.println("发送失败");
}
}
}
package com.lin.utils;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class StreamTools {
/*
* 读取流中的数据
*
* */
public static byte[] read(InputStream inStream) throws Exception{
byte[] buffer=new byte[1024];
ByteArrayOutputStream stream=new ByteArrayOutputStream();
int len=0;
while((len=inStream.read(buffer))!=-1){
stream.write(buffer,0,len);
}
inStream.close();
return stream.toByteArray();
}
}
video.xml
<?xml version="1.0" encoding="UTF-8"?>
<person>
<namme id="1">
<height>182</height>
<weight>67</weight>
</namme>
<namme id="2">
<height>180</height>
<weight>62</weight>
</namme>
</person>
效果:web应用部署到模拟器后
运行XMLText为java应用
console:
发送成功
打开服务器的consloe
打印
相关推荐
post提交工具增强版 post提交工具增强版 post提交工具增强版 post提交工具增强版
自己写的Post提交工具,用于编写接受post参数的页面调试使用。工具需要.net环境,共编译3个版本,分别为3.5,4.0,4.5。 工具支持选择请求编码,选择响应编码,支持选择请求内容类型,支持上传文件。若服务器返回错误...
易语言post提交示例,易语言post提交示例易语言post提交示例易语言post提交示例易语言post提交示例
易语言post提交网页数据,易语言post提交网页数据
以前在面试的时候经常遇到问关于跨域的事儿,所以自己对跨域有一定的概念性了解,知道什么是跨域以及解决跨域的方法,但是具体实际从来没有操作过,直到最近在公司项目中,遇到了一个需要使iframe跨域进行POST提交的...
POST提交数据加密使用HttpWatch获取的数据是加密后的 用户名user1 密码:123456
http post 提交工具,可以查看返回结果
接口开发:中httppost 提交方式的详细描述
简单的基于http。post提交的暴力破解脚本,可破解常见的用户登陆
post提交测试工具
asp.net网页post提交的详细介绍。 asp.net网页post提交的详细介绍。
如何使用post提交网页表单中的数据,vb使用post提交数据
通过js模拟post提交 1:请求需要的参数过长,超过get允许的最大长度 2:想要隐藏地址栏的参数 //新创建一个form表单 [removed]('<form name=myForm></form>'); var myForm=document.forms['myForm']; ...
VB开发的通过httppost向服务器提交数据。Http协议Post提交。VB开发的通过httppost向服务器提交数据。Http协议Post提交
c# post 提交数据 测试通过 上传代码
HttpClient Post提交多文件及多个普通参数,已经封装成工具类。 需传入 要请求的url 普通参数map 例 map.put("param1","张三"); 需要传入的文件流map 其中key为文件名 服务端接收无乱码。
对于刚接触用C#写一些自动登录的人来说首先要了解的就是如何传递数据,这里介绍了post提交方式的简单运用,
Ajax使用POST提交中文乱码问题.docx