post 方式提交XML文件调用接口
2012-07-19 10:31
295 查看
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
public class Test {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
//直接字符串拼接
StringBuffer sb = new StringBuffer();
sb.append("<app_ei_sync_req><enabler_id>pengxwtest</enabler_id><dev_id>pengxwtest</dev_id>" +
"<app_id>pengxwtest</app_id><app_secret>pengxwtest</app_secret>" +
"<app_status>2</app_status><app_level>0</app_level><app_ei><ei_id>1</ei_id>" +
"<ei_id>2</ei_id><ei_id>3</ei_id></app_ei></app_ei_sync_req>");//xml数据存储
String data = sb.toString();
String url = "接口地址";
HttpClient httpclient = new HttpClient();
PostMethod post = new PostMethod(url);
String info = null;
try {
RequestEntity entity = new StringRequestEntity(data, "text/xml",
"iso-8859-1");
post.setRequestEntity(entity);
httpclient.executeMethod(post);
int code = post.getStatusCode();
if (code == HttpStatus.SC_OK)
info = new String(post.getResponseBodyAsString()); //接口返回的信息
} catch (Exception ex) {
ex.printStackTrace();
} finally {
post.releaseConnection();
}
System.out.println(info);
}
//读取xml文件
public class xmlTool(){
InputStreamReader read = new InputStreamReader (new FileInputStream("f://aa.xml"),"UTF-8");
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(read);
String row;
while((row = br.readLine())!=null){
sb.append(row.trim());
}
String data = sb.toString();
String url = "http://localhost:9099/vtoss/cloudapi/rp_video_transcode_batch.do";
HttpClient httpclient = new HttpClient();
PostMethod post = new PostMethod(url);
String info = null;
try {
RequestEntity entity = new StringRequestEntity(data, "text/xml",
"UTF-8");
post.setRequestEntity(entity);
httpclient.executeMethod(post);
int code = post.getStatusCode();
if (code == HttpStatus.SC_OK)
info = new String(post.getResponseBodyAsString());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
post.releaseConnection();
}
System.out.println(info);
}
}
import java.io.InputStreamReader;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
public class Test {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
//直接字符串拼接
StringBuffer sb = new StringBuffer();
sb.append("<app_ei_sync_req><enabler_id>pengxwtest</enabler_id><dev_id>pengxwtest</dev_id>" +
"<app_id>pengxwtest</app_id><app_secret>pengxwtest</app_secret>" +
"<app_status>2</app_status><app_level>0</app_level><app_ei><ei_id>1</ei_id>" +
"<ei_id>2</ei_id><ei_id>3</ei_id></app_ei></app_ei_sync_req>");//xml数据存储
String data = sb.toString();
String url = "接口地址";
HttpClient httpclient = new HttpClient();
PostMethod post = new PostMethod(url);
String info = null;
try {
RequestEntity entity = new StringRequestEntity(data, "text/xml",
"iso-8859-1");
post.setRequestEntity(entity);
httpclient.executeMethod(post);
int code = post.getStatusCode();
if (code == HttpStatus.SC_OK)
info = new String(post.getResponseBodyAsString()); //接口返回的信息
} catch (Exception ex) {
ex.printStackTrace();
} finally {
post.releaseConnection();
}
System.out.println(info);
}
//读取xml文件
public class xmlTool(){
InputStreamReader read = new InputStreamReader (new FileInputStream("f://aa.xml"),"UTF-8");
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(read);
String row;
while((row = br.readLine())!=null){
sb.append(row.trim());
}
String data = sb.toString();
String url = "http://localhost:9099/vtoss/cloudapi/rp_video_transcode_batch.do";
HttpClient httpclient = new HttpClient();
PostMethod post = new PostMethod(url);
String info = null;
try {
RequestEntity entity = new StringRequestEntity(data, "text/xml",
"UTF-8");
post.setRequestEntity(entity);
httpclient.executeMethod(post);
int code = post.getStatusCode();
if (code == HttpStatus.SC_OK)
info = new String(post.getResponseBodyAsString());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
post.releaseConnection();
}
System.out.println(info);
}
}
相关文章推荐
- post 方式提交XML文件调用接口
- POST方式提交XML调用接口
- 在PHP中模拟post提交方式,调用JSON接口_php调用json接口
- php模拟post提交请求,调用接口
- JAVA 调用HTTP接口POST或GET实现方式
- 详解php用curl调用接口方法,get和post两种方式
- 【转】java通用URL接口地址调用方式GET和POST方式
- php模拟post提交请求,调用接口
- php模拟post提交请求,调用接口
- HttpClient Get/Post方式调用Http接口
- get/post方式调用http接口
- get/post方式调用http接口
- restful接口客户端和服务端开发,HttpURLConnection,HttpClient,post ,get方式调用
- AutoCAD.NET 不使用P/Invoke方式调用acad.exe或accore.dll中的接口(如acedCommand、acedPostCommand等)
- get/post方式调用http接口
- vb.net 接口POST方式传参数提交返回值
- Http使用post方式提交数据(使用apache标准接口)
- php模拟post提交请求,调用接口
- Java 以post请求方式通过json格式调用Webservice接口
- get/post方式调用http接口