Java发送HTTP POST请求(内容为xml格式)
来源:互联网 发布:c语言头文件下载 网盘 编辑:程序博客网 时间:2024/05/12 02:07
服务器地址: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 POST请求(内容为xml格式)
- Java发HTTP POST请求(内容为xml格式)
- Java发HTTP POST请求(内容为xml格式)
- Java发HTTP POST请求(内容为xml格式)
- Java发HTTP POST请求(内容为xml格式)
- Java发HTTP POST请求(内容为xml格式)
- Java发HTTP POST请求(内容为xml格式)
- java发送http post请求,格式为xml
- java发送http,内容为xml格式
- 对于java用发送http请求,请求内容为xml格式
- 对于java用发送http请求,请求内容为xml格式
- java发送xml格式HTTP请求
- Http post 请求发送 xml
- java 发送 json、xml格式的 http请求,并读取响应response内容实例
- JAVA HTTP POST请求(请求参数格式为json)
- java发送JSON格式的http通讯的post请求
- JAVA发送HTTP请求(post、get),读取HTTP响应内容,实例及应用
- 行为型模式-观察者模式之监听器
- ActionScript语法基础1
- UML用例图总结
- linux 进程地址空间
- 雅虎各国天气代码地址
- Java发送HTTP POST请求(内容为xml格式)
- 为什么构造函数没有返回值?
- 多线程调用COM组件,使用CoInitializeEx()
- 网页压缩gzip的问题及说明教程
- 子网掩码,子网划分,以及能创建的子网个数
- php调试技术
- 非常有用!eclipse与myeclipse恢复已删除的文件和代码
- 如何导出SHP文件中的点坐标(x,y)
- String.Format用法