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 ...
随机推荐
- A - FatMouse' Trade
Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the wareho ...
- uva424 高精度计算
题意是计算各长整数的和,最多输入100个数,每个数都是正整数且位数不超过100. 很明显100位的数用数组存会方便许多,然后设置两个整型数组,一个存进位,一个存结果.为了对齐进行运算,我将所有的数先逆 ...
- ExpandableListView 箭头样式
ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组,每组里 又可包含多个列表项.ExpandableListVive ...
- jQ插件编写
参考文档:http://www.cnblogs.com/Dlonghow/p/4142034.html 编写插件最先接触到的就是jQuery.fn.extend 和jQuery.extend 这个两个 ...
- tomcat部署不正确
我把一个名为cp的web项目导入myeclipse中,由于要再次导入该工程的新版本,我就把旧版本的cp项目改名成cp3.但是在运行新项目cp的时候,tomcat出问题,一直是之前旧cp对应的tomca ...
- 菱形java代码
public class boy { //菱形 public static void main(String[] args) { int m=4; for (int i=0;i<=m;i++){ ...
- Java图形化界面设计——布局管理器之null布局(空布局)
一般容器都有默认布局方式,但是有时候需要精确指定各个组建的大小和位置,就需要用到空布局. 操作方法: 1) 首先利用setLayout(null)语句将容器的布局设置为null布局(空布局 ...
- OpenGL 4 : 一个漂亮的心 For you, My Love
画一个心形有非常多公式能够使用,以下这个公式我觉得最完美了: float x = R * 16 * pow(sin(theta), 3); float y = R * (13 * cos(theta) ...
- POJ1363:Rails
Description There is a famous railway station in PopPush City. Country there is incredibly hilly. Th ...
- hdu 2203亲和串 (kmp)
#include<cstdio>#include<iostream>#include<cstring>#include<string>using nam ...