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

该博客展示了如何使用Java的Apache Commons HTTPClient库通过POST方式发送XML数据来调用API接口。首先,代码创建了一个XML字符串,然后构造了一个PostMethod对象,设置请求实体为XML字符串,并执行HTTP请求。如果响应状态码为200,说明请求成功,打印接口返回的信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);

 

 

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值