如何用C++通过http协议 发送xml数据给服务器端
1个回答
推荐于2016-03-16
展开全部
public void sendXmlTest() throws Exception{
//通过类装载器装载XML资源
InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("test.xml");
byte[] xml=StreamTool.read(inputStream);
String path="http://172.22.35.112:8080/videonews/GetXmlInfo";
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setDoOutput(true);
//设置HTTP请求的头字段
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); //内容类型
conn.setRequestProperty("Content-Length", String.valueOf(xml.length)); //实体内容的长度
conn.getOutputStream().write(xml); //通过输出流把数据写到服务器
if(conn.getResponseCode()==200){
System.out.println("发送成功!");
}else{
System.out.println("发送失败!");
}
}
服务器端:
byte[] xml=StreamTool.read(request.getInputStream()); //获得输出流
System.out.println(new String(xml,"UTF-8"));
//通过类装载器装载XML资源
InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("test.xml");
byte[] xml=StreamTool.read(inputStream);
String path="http://172.22.35.112:8080/videonews/GetXmlInfo";
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setDoOutput(true);
//设置HTTP请求的头字段
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); //内容类型
conn.setRequestProperty("Content-Length", String.valueOf(xml.length)); //实体内容的长度
conn.getOutputStream().write(xml); //通过输出流把数据写到服务器
if(conn.getResponseCode()==200){
System.out.println("发送成功!");
}else{
System.out.println("发送失败!");
}
}
服务器端:
byte[] xml=StreamTool.read(request.getInputStream()); //获得输出流
System.out.println(new String(xml,"UTF-8"));
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询