Web Services 如何从某个Web服务发送(和接收)XML格式的请求?

cetgtptt  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(167)

我在一个电子商务网站上工作,使用JSF 2。为了与银行进行所有操作的公司进行通信,我需要向他们发送以下XML(这只是他们提供的一个示例):

<?xml version="1.0" encoding="ISO-8859-1"?>
<requisicao-transacao versao="1.2.0" id="6560a94c-663b-4aec-9a45-e45f278e00b4" xmlns="http://ecommerce.cbmp.com.br">
    <dados-ec>
        <numero>1001734898</numero>
        <chave>e84827130b9837473681c2787007da5914d6359947015a5cdb2b8843db0fa832</chave>
    </dados-ec>
    <dados-pedido>
        <numero>1603662828</numero>
        <valor>100</valor>
        <moeda>986</moeda>
        <data-hora>2010-07-14T15:50:11</data-hora>
        <idioma>PT</idioma>
    </dados-pedido>
    <forma-pagamento>
        <bandeira>visa</bandeira>
        <produto>A</produto>
        <parcelas>1</parcelas>
    </forma-pagamento>
    <url-retorno>https://www.dummyurl.du/dummypage.do?id=trhjgnerifvnidjfnvmd</url-retorno>
    <autorizar>1</autorizar>
    <capturar>true</capturar>
</requisicao-transacao>

因此,在阅读了大量有关如何发送和接收XML的内容之后,我创建了以下方法:

public String rent(){
    //String folderAndFile = createTransaction();

    //creating the HTTP Post
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("https://qasecommerce.cielo.com.br/servicos/ecommwsec.do");

    try {
       //Reading the file as an entity
        FileEntity entity = new FileEntity(new File("/home/valter.silva/sample.xml"));
        entity.setContentType("text/xml");
        post.setEntity(entity);

        HttpResponse response = client.execute(post);
        HttpEntity httpEntity = response.getEntity();

        System.out.println(EntityUtils.toString(httpEntity));

    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

但输出始终为:

INFO: <?xml version="1.0" encoding="ISO-8859-1"?> <erro xmlns="http://ecommerce.cbmp.com.br"> <codigo>001</codigo> <mensagem>Requisição inválida</mensagem> </erro>

这意味着我发送的.xml是无效的。由于某种原因,XML是错误的...但是什么呢?
我发送文件的方式没问题吧?我能做些什么呢?

update我尝试了另一种方法,但输出仍然是相同的,...,我的代码有什么问题吗?

//approach v1
    public String rent(){
            //String folderAndFile = createTransaction();

            try {
                File file = new File("/home/valter.silva/test.xml");
                HttpPost post = new HttpPost("https://qasecommerce.cielo.com.br/servicos/ecommwsec.do");
                post.setEntity(new InputStreamEntity(new FileInputStream(file),file.length()));
                post.setHeader("Content-type", "text/xml; charset=ISO-8859-1");

                //creating the HTTP Post
                DefaultHttpClient client = new DefaultHttpClient();

                HttpResponse response = client.execute(post);
                HttpEntity httpEntity = response.getEntity();

                System.out.println(EntityUtils.toString(httpEntity));

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

//approach v2
public String rent(){
        //String folderAndFile = createTransaction();

        try {
            File file = new File("/home/valter.silva/test.xml");
            HttpPost post = new HttpPost("https://qasecommerce.cielo.com.br/servicos/ecommwsec.do");

            //creating the HTTP Post
            DefaultHttpClient client = new DefaultHttpClient();

            String fileInString = fileToString("/home/valter.silva/test.xml");
            InputStream inputStream=new ByteArrayInputStream(fileInString.getBytes());//init your own inputstream
            InputStreamEntity inputStreamEntity=new InputStreamEntity(inputStream,fileInString.length());
            post.setEntity(inputStreamEntity);

            HttpResponse response = client.execute(post);
            HttpEntity httpEntity = response.getEntity();

            System.out.println(EntityUtils.toString(httpEntity));

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
i2loujxw

i2loujxw1#

你能检查一下你要发布的url是否能正确处理你的xml吗?我试着用简单的http post把你提供的xml上传到指定的url,结果得到了

<?xml version="1.0" encoding="ISO-8859-1"?> <erro xmlns="http://ecommerce.cbmp.com.br"> <codigo>001</codigo> <mensagem>Requisição inválida</mensagem> </erro>

我更喜欢你先尝试从外面上传xml,然后用你的代码试试。例如,我用了Mozilla addon的RESTClient。

相关问题