POST 提交XML 文件

本文提供了一个使用Java实现HTTP请求与响应中XML数据交互的例子。包括发送POST请求并附带XML格式的数据,以及如何从HTTP请求中读取并打印XML数据。

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

1、请求

import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;


public class ctrlDemo {

    private HttpClient httpClient;

    @Before
    public void setUp() {
        httpClient = HttpClientBuilder.create().build();
    }

    @Test
    public void test() throws Exception {
        String url = "http://";

        String xml = loadData("***.xml");

        BaseResp baseReqsp = executePost(url, xml, httpClient);
        assertTrue(baseReqsp.isSuccess());
    }

    private String loadData(String path) throws IOException {
        ClassPathResource resource = new ClassPathResource(path);
        return IOUtils.toString(resource.getInputStream());
    }
    
    public static void executePost(String url, String body, HttpClient httpClient) throws Exception{
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader(HTTP.CONTENT_TYPE, "application/xml");//请求参数为XML格式 
        
        StringEntity entity = new StringEntity(body, "UTF-8");  
        httpPost.setEntity(entity);  
        HttpResponse resp = httpClient.execute(httpPost);
        HttpEntity respEntity = resp.getEntity();
        String text = EntityUtils.toString(respEntity, "UTF-8");
    }

}

2、接收

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

	@RequestMapping(value = "/xml/请求路径", method = RequestMethod.POST)
    public void order(HttpServletRequest request, HttpServletResponse response) {
        
            String xmlReq = IOUtils.toString(request.getInputStream());
            System.out.println("data: {}", xmlReq);

			// unmarshal : 使用 JAXB 解析 (具体如何解析请看 http://www.oschina.net/question/tag/jaxb )
			//Object : 代表需要解析的JAVA类
            Object data = unmarshal(xmlReq, Object.class, false);
            

    }


要使用curl命令进行POST请求发送XML文件,可以按照以下步骤进行操作: 1. 首先,将XML文件的内容保存到一个文件中,比如1.xml。 2. 然后,使用curl命令执行POST请求,并指定文件的路径作为数据参数。命令的格式如下: ``` cat 1.xml | curl -X POST -H 'Content-type:text/xml' -d @- http://1.1.1.1::8081/httpInvokeServlet ``` 这个命令会使用cat命令将1.xml文件的内容读取并通过管道传递给curl命令的-d参数。同时,通过-H参数指定请求头的Content-type为text/xml。 3. 替换命令中的1.xml为你实际的XML文件路径和文件名。 总结起来,使用curl命令发送POST请求并提交XML文件的步骤如下: 1. 将XML文件保存到一个文件中,比如1.xml。 2. 使用命令`cat 1.xml | curl -X POST -H 'Content-type:text/xml' -d @- http://1.1.1.1::8081/httpInvokeServlet`发送POST请求,并将文件内容作为数据参数传递给curl命令。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [curl模拟post json或post xml文件](https://blog.youkuaiyun.com/db9902943/article/details/101241250)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [如何用Curl 来post xml 数据](https://blog.youkuaiyun.com/weixin_30872671/article/details/95253137)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戴子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值