HttpURL连接设置POST参数URL
HttpURLConnection setting POST parameter URL
我正在尝试访问 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 header 是这样显示的:
POST /x/y/z/a/b/c/d/myUrlWS.jws
...(reset of http header)...
SOAP Message
我从服务器收到一个错误,我认为 http 中的 POST 应该是这样的:
POST /
或
POST /action
或
POST /myUrlWS.jws
所以我不知道如何更改header中的POST参数。如何在不更改连接的情况下做到这一点 URL/address?
编辑 - 来自服务器的 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
-> 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
通过查看您获得的异常,我可以猜测您的 SOAP 负载消息格式不正确。
XML 消息的每个标记都应由命名空间提及。在您的情况下,您在以下标记中将命名空间称为 "WL5G3N1"。
<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>
我正在尝试访问 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 header 是这样显示的:
POST /x/y/z/a/b/c/d/myUrlWS.jws
...(reset of http header)...
SOAP Message
我从服务器收到一个错误,我认为 http 中的 POST 应该是这样的:
POST /
或
POST /action
或
POST /myUrlWS.jws
所以我不知道如何更改header中的POST参数。如何在不更改连接的情况下做到这一点 URL/address?
编辑 - 来自服务器的 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
-> 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
通过查看您获得的异常,我可以猜测您的 SOAP 负载消息格式不正确。
XML 消息的每个标记都应由命名空间提及。在您的情况下,您在以下标记中将命名空间称为 "WL5G3N1"。
<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>