HttpURLConnection设置POST参数URL

时间:2015-04-08 17:43:54

标签: java http soap httpurlconnection

我正在尝试访问SOAP服务。为此,我手动创建SOAP请求(我首选)。问题在于,当我做

URL urli = new URL("http://www.myserver.com//x/y/z/a/b/c/d/myUrlWS.jws");
HttpURLConnection c = (HttpURLConnection) urli.openConnection();
c.setRequestMethod("POST");
c.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
c.setRequestProperty("Content-Length", bytes.length+"");
c.setRequestProperty("SOAPAction", "");
...
c.getInputStream();

问题是http标题以这种方式显示:

POST /x/y/z/a/b/c/d/myUrlWS.jws
...(reset of http header)...

SOAP Message

我从服务器收到错误,我认为http中的POST应该是:

POST /

POST /action

POST /myUrlWS.jws

所以我不知道如何更改标题中的POST参数。如何在不更改连接URL /地址的情况下执行此操作?

编辑 - 来自服务器的SOAP回复

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Header/><env:Body><env:Fault><faultcode>env:Server</faultcode><faultstring>[Server CodecHandler] Failed to decode 
 -&gt; Unable to find xml element for parameter: documentos
</faultstring><detail><java:string xmlns:java="java.io">weblogic.wsee.codec.CodecException: Unable to find xml element for parameter: documentos
</java:string></detail></env:Fault></env:Body></env:Envelope>

完整的HTTP请求

    POST /x/y/z/a/b/c/d/myUrlWS.jws HTTP/1.1
    Content-Type: text/xml; charset=utf-8
    SOAPAction: 
    User-Agent: Java/1.7.0_75
    Host: wwwW.somehost.some.gov.br
    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Connection: keep-alive
    Content-Length: 2026

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:WL5G3N0="http://schemas.xmlsoap.org/wsdl/" xmlns:WL5G3N1="http://www.openuri.org/" xmlns:WL5G3N2="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:WL5G3N3="http://www.openuri.org/2006/12/wsdl/upgradedJWS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <SOAP-ENV:Body>
            <WL5G3N1:enviarDados xmlns:WL5G3N1="http://www.openuri.org/">
                <emitente>
                    <CNPJEmitente>11111111</CNPJEmitente>
                    <Email>my@email.com</Email>
                </emitente>
                <documentos>
                    <Documento>
                        <TipoPagamento>1</TipoPagamento>
                        <TipoDocumento>2</TipoDocumento>
                        <DataPagamento>13/04/15</DataPagamento>
                        <ItensPagamentos>
                            <ItemPagamento>
                                <TipoId>1</TipoId>
                                <Cnpj>1111111111</Cnpj>
                                <CodigoProduto>11111</CodigoProduto>
                                <DataFatoGerador>13/04/15</DataFatoGerador>
                                <DataVencimento>13/04/15</DataVencimento>
                                <DddContribuinte>16</DddContribuinte>
                                <EnderecoContribuinte>SOME NAME</EnderecoContribuinte>
                                <MunicipioContribuinte>NAME</MunicipioContribuinte>
                                <UFContribuinte>SP</UFContribuinte>
                                <CepContribuinte>11111111</CepContribuinte>
                                <TelefoneContribuinte>111111</TelefoneContribuinte>
                                <Natureza>1</Natureza>
                                <NomeRazaoSocial>SOME NAME</NomeRazaoSocial>
                                <NotaFiscalCnpj>1111111</NotaFiscalCnpj>
                                <NotaFiscalDataEmissao>2014-12-04</NotaFiscalDataEmissao>
                                <NotaFiscalNumero>111111</NotaFiscalNumero>
                                <NotaFiscalSerie>1</NotaFiscalSerie>
                                <NotaFiscalTipo>NF-e</NotaFiscalTipo>
                                <NumControleContribuinte>111111</NumControleContribuinte>
                                <TipoApuracao>2</TipoApuracao>
                                <PeriodoReferenciaAno>2015</PeriodoReferenciaAno>
                                <PeriodoReferenciaMes>04</PeriodoReferenciaMes>
                                <PeriodoReferenciaDecendio>2</PeriodoReferenciaDecendio>
                                <DiaVencimento>13/04/15</DiaVencimento>
                                <ValorICMSPrincipal>221.21</ValorICMSPrincipal>
                                <ValorTotal>221.21</ValorTotal>
                            </ItemPagamento>
                        </ItensPagamentos>
                    </Documento>
                </documentos>
            </WL5G3N1:enviarDados>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

WSDL在这里: WSDL

1 个答案:

答案 0 :(得分:1)

通过查看您获得的exeption,我可以猜测您的SOAP有效负载消息格式不正确。

命名空间应该提到XML消息的每个标记。在您的情况下,您在以下标记中提到名称空间为&#34; WL5G3N1&#34;

<WL5G3N1:enviarDados xmlns:WL5G3N1="http://www.openuri.org/">

因此,您应该在整个消息中使用WL5G3N1来引用每个XML元素。

所以下面的XML应该可以工作。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:WL5G3N0="http://schemas.xmlsoap.org/wsdl/" xmlns:WL5G3N1="http://www.openuri.org/"
    xmlns:WL5G3N2="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:WL5G3N3="http://www.openuri.org/2006/12/wsdl/upgradedJWS"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SOAP-ENV:Body>
        <WL5G3N1:enviarDados xmlns:WL5G3N1="http://www.openuri.org/">
            <WL5G3N1:emitente>
                <WL5G3N1:CNPJEmitente>11111111</WL5G3N1:CNPJEmitente>
                <WL5G3N1:Email>my@email.com</WL5G3N1:Email>
            </WL5G3N1:emitente>
            <WL5G3N1:documentos>
                <WL5G3N1:Documento>
                    <WL5G3N1:TipoPagamento>1</WL5G3N1:TipoPagamento>
                    <WL5G3N1:TipoDocumento>2</WL5G3N1:TipoDocumento>
                    <WL5G3N1:DataPagamento>13/04/15</WL5G3N1:DataPagamento>
                    <WL5G3N1:ItensPagamentos>
                        <WL5G3N1:ItemPagamento>
                            <WL5G3N1:TipoId>1</WL5G3N1:TipoId>
                            <WL5G3N1:Cnpj>1111111111</WL5G3N1:Cnpj>
                            <WL5G3N1:CodigoProduto>11111</WL5G3N1:CodigoProduto>
                            <WL5G3N1:DataFatoGerador>13/04/15</WL5G3N1:DataFatoGerador>
                            <WL5G3N1:DataVencimento>13/04/15</WL5G3N1:DataVencimento>
                            <WL5G3N1:DddContribuinte>16</WL5G3N1:DddContribuinte>
                            <WL5G3N1:EnderecoContribuinte>SOME NAME</WL5G3N1:EnderecoContribuinte>
                            <WL5G3N1:MunicipioContribuinte>NAME</WL5G3N1:MunicipioContribuinte>
                            <WL5G3N1:UFContribuinte>SP</WL5G3N1:UFContribuinte>
                            <WL5G3N1:CepContribuinte>11111111</WL5G3N1:CepContribuinte>
                            <WL5G3N1:TelefoneContribuinte>111111
                            </WL5G3N1:TelefoneContribuinte>
                            <WL5G3N1:Natureza>1</WL5G3N1:Natureza>
                            <WL5G3N1:NomeRazaoSocial>SOME NAME</WL5G3N1:NomeRazaoSocial>
                            <WL5G3N1:NotaFiscalCnpj>1111111</WL5G3N1:NotaFiscalCnpj>
                            <WL5G3N1:NotaFiscalDataEmissao>2014-12-04</WL5G3N1:NotaFiscalDataEmissao>
                            <WL5G3N1:NotaFiscalNumero>111111</WL5G3N1:NotaFiscalNumero>
                            <WL5G3N1:NotaFiscalSerie>1</WL5G3N1:NotaFiscalSerie>
                            <WL5G3N1:NotaFiscalTipo>NF-e</WL5G3N1:NotaFiscalTipo>
                            <WL5G3N1:NumControleContribuinte>111111
                            </WL5G3N1:NumControleContribuinte>
                            <WL5G3N1:TipoApuracao>2</WL5G3N1:TipoApuracao>
                            <WL5G3N1:PeriodoReferenciaAno>2015</WL5G3N1:PeriodoReferenciaAno>
                            <WL5G3N1:PeriodoReferenciaMes>04</WL5G3N1:PeriodoReferenciaMes>
                            <WL5G3N1:PeriodoReferenciaDecendio>2
                            </WL5G3N1:PeriodoReferenciaDecendio>
                            <WL5G3N1:DiaVencimento>13/04/15</WL5G3N1:DiaVencimento>
                            <WL5G3N1:ValorICMSPrincipal>221.21</WL5G3N1:ValorICMSPrincipal>
                            <WL5G3N1:ValorTotal>221.21</WL5G3N1:ValorTotal>
                        </WL5G3N1:ItemPagamento>
                    </WL5G3N1:ItensPagamentos>
                </WL5G3N1:Documento>
            </WL5G3N1:documentos>
        </WL5G3N1:enviarDados>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>