java post 提交xml_post 方式提交XML文件调用接口

该博客展示了如何在Java中通过POST方法提交XML数据来调用API接口。首先,通过字符串拼接创建XML数据,然后使用HttpClient和PostMethod执行HTTP POST请求。代码包括处理响应并检查状态码,确保请求成功。此外,还提供了一个读取XML文件并进行相同提交的示例。
摘要由CSDN通过智能技术生成

importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.util.Date;importjava.util.HashMap;importjava.util.List;importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.HttpStatus;importorg.apache.commons.httpclient.methods.PostMethod;importorg.apache.commons.httpclient.methods.RequestEntity;importorg.apache.commons.httpclient.methods.StringRequestEntity;public classTest {/***@paramargs*/

public static void main(String[] args) throwsException{//直接字符串拼接

StringBuffer sb = newStringBuffer();

sb.append("pengxwtestpengxwtest" +

"pengxwtestpengxwtest" +

"201" +

"23");//xml数据存储

String data =sb.toString();

String url= "接口地址";

HttpClient httpclient= newHttpClient();

PostMethod post= newPostMethod(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 classxmlTool(){

InputStreamReader read= new InputStreamReader (new FileInputStream("f://aa.xml"),"UTF-8");

StringBuffer sb= newStringBuffer();

BufferedReader br= newBufferedReader(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= newHttpClient();

PostMethod post= newPostMethod(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= newString(post.getResponseBodyAsString());

}catch(Exception ex) {

ex.printStackTrace();

}finally{

post.releaseConnection();

}

System.out.println(info);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值