简体   繁体   中英

How to send a request (and receive it) in XML format from some web-service?

I'm working in a E-Commerce website, with JSF 2. In order to communicate with the company that makes all the operation with the banks, I need to send this XML to them (it's just a sample provided from them):

<?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>

So after reading a lot about how to send and XML and receive it, I create this method:

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

But the output is always :

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>

Which means that my .xml that I'm sending is invalid. That for some reason, the XML is wrong.. but what ?

Is alright the way that I'm sending the file ? What can I do about it ?

update I was trying another approach but still the output is always the same, ..., is something wrong with my code ?

//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;
    }

Can you check that the url where you trying to post can handle your xml correctly ? I have tried to upload the xml you provided using just simple http post to the specified url and got

<?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>

I prefer you first try to upload the xml from outside and then try with your code . For example i used RESTClient of Mozilla addon .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM