Java发送HTTP POST请求(内容为xml格式)
今天在给平台用户提供http简单接口的时候,顺便写了个调用的Java类供他参考。
服务器地址:http://5.0.217.50:17001/VideoSend
服务器提供的是xml格式的http接口,接口定义如下:
<!--视频点送: videoSend-->
<videoSend>
<header>
<sid>%s</sid>
<type>service</type>
</header>
<service name="videoSend">
<fromNum>%s</fromNum>
<toNum>%s</toNum> <!--需要接通的用户的电话号码 -->
<videoPath>%s</videoPath> <!--视频文件路径 -->
<chargeNumber>%s</chargeNumber> <!--计费号码 -->
</service>
</videoSend>


<!--视频点送返回结果: videoSendResult-->
<videoSend>
<header>
<sid>%s</sid>
<type>service</type>
</header>
<service name="videoSendResult">
rescode>%s</rescode> <!--0000:视频点送成功,0001:请求参数信息错误, 0002:接通用户失败-->
</service>
</videoSend>
对应调用端的Java代码(只是个demo,参数都暂时写死了)如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpPostTest {
void testPost(String urlStr) {
try {
URL url = new URL(urlStr);
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setRequestProperty("Pragma:", "no-cache");
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Content-Type", "text/xml");

OutputStreamWriter out = new OutputStreamWriter(con
.getOutputStream());
String xmlInfo = getXmlInfo();
System.out.println("urlStr=" + urlStr);
System.out.println("xmlInfo=" + xmlInfo);
out.write(new String(xmlInfo.getBytes("ISO-8859-1")));
out.flush();
out.close();
BufferedReader br = new BufferedReader(new InputStreamReader(con
.getInputStream()));
String line = "";
for (line = br.readLine(); line != null; line = br.readLine()) {
System.out.println(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private String getXmlInfo() {
StringBuilder sb = new StringBuilder();
sb.append("<videoSend>");
sb.append(" <header>");
sb.append(" <sid>1</sid>");
sb.append(" <type>service</type>");
sb.append(" </header>");
sb.append(" <service name=\"videoSend\">");
sb.append(" <fromNum>0000021000011001</fromNum>");
sb.append(" <toNum>33647405</toNum>");
sb.append(" <videoPath>mnt/5.0.217.50/resources/80009.mov</videoPath>");
sb.append(" <chargeNumber>0000021000011001</chargeNumber>");
sb.append(" </service>");
sb.append("</videoSend>");
return sb.toString();
}

public static void main(String[] args) {
String url = "http://5.0.217.50:17001/VideoSend";
new HttpPostTest().testPost(url);
}
}
Java发送HTTP POST请求(内容为xml格式)的更多相关文章
- 对于java用发送http请求,请求内容为xml格式
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...
- java发送http请求,内容为xml格式&&传统URI类请求
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...
- Java发HTTP POST请求(内容为xml格式)
Java发HTTP POST请求(内容为xml格式) 一.POST请求 服务器地址:http://5.0.217.50:17001/VideoSend 服务器提供的是xml格式的http接口,接口定义 ...
- ajax数据请求4(xml格式)
ajax数据请求4(xml格式): <!doctype html> <html> <head> <meta charset="utf-8" ...
- Java发送HTTP POST请求示例
概述: http请求在所有的编程语言中几乎都是支持的,我们常用的两种为:GET,POST请求.一般情况下,发送一个GET请求都很简单,因为参数直接放在请求的URL上,所以,对于PHP这种语言,甚至只需 ...
- java发送http get请求的两种方式
长话短说,废话不说 一.第一种方式,通过HttpClient方式,代码如下: public static String httpGet(String url, String charset) thro ...
- Java 发送http post 请求
package com.sm.utils; import java.io.BufferedReader; import java.io.InputStreamReader; import java.i ...
- Soup协议-即普通post请求,内容域xml
1.基础问题 1.1 soup-Simple Object Access Protocal简单对象访问协议 a).承载在http协议之上,http支持传输img/html/文件等,soup请求和响应域 ...
- java 发送get,post请求
package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...
随机推荐
- mina 实例(转载)
mina:http://mina.apache.org/ 原文:http://maosheng.iteye.com/blog/1891665 大并发量socket 通信框架MINA介绍 博客分类: J ...
- BZOJ 2946: [Poi2000]公共串( 后缀自动机 )
一个串建后缀自动机, 其他串在上面跑, 然后用当前串跑的去更新全部 ------------------------------------------------------------------ ...
- Hadoop学习之Hadoop集群搭建
1.检查网络状况 Dos命令:ping ip地址,同时,在Linux下通过命令:ifconfig可以查看ip信息2.修改虚拟机的ip地址 打开linux网络连接,在桌面右上角,然后编辑ip地址, ...
- Windows上部署Redis
http://www.cnblogs.com/gaobing/p/5026136.html
- Angular form
参考 http://blog.xebia.com/2013/10/15/angularjs-validating-radio-buttons/ http://stackoverflow.com/que ...
- 转: Nodejs 发送HTTP POST请求实例
项目里面需要用到使用NodeJs来转发HTTP POST请求,把过程记录一下: exports.sendEmail = function (req, res) { res.send(200, req. ...
- java 反射提取类信息, 动态代理 和过滤某些方法演示
java 反射提取类信息, 动态代理 和过滤某些方法演示 package org.rui.classts.reflects; import java.lang.reflect.Constructor; ...
- linux进程之fork 和 exec函数
---恢复内容开始--- fork函数 该函数是unix中派生新进程的唯一方法. #include <unistd.h> pid_t fork(void); 返回: (调用它一次, 它 ...
- Javascript Duff装置 循环展开(Javascript Loop unrolling Duff device)
Javascript 中会用到for 循环,当要循环的数据记录很多的时候,可能会对性能产生很大影响.这时我们可以考虑展开for循环,这时就要用到Duff装置(Duff Device). 先来看一个小例 ...
- 延迟N秒执行某个方法
[self performSelector:<#(nonnull SEL)#> withObject:<#(nullable id)#> afterDelay:<#(NS ...