C#Web服务与wss安全绿洲标头一起使用

时间:2018-08-25 14:25:48

标签: c# web-services wcf invoices

我是Web服务中的新手,我不得不使用Web服务发送文件并捕获响应,我的问题是我无法生成有效的SOAP Header。我正在使用C#WCF Visual Studio 2017

要使用的Web服务是:https://facturaelectronica.dian.gov.co/habilitacion/B2BIntegrationEngine/FacturaElectronica/facturaElectronica.wsdl

根据提供商提供的文档,标头的规范为:

使用Base64的请求示例

POST /B2BIntegrationEngine/FacturaElectronica HTTP/1.1 Accept-Encoding: gzip,deflate Content-Type: text/xml;charset=UTF-8 SOAPAction: "" Content-Length: 3342 Host: 192.168.250.65:9080 Connection: Keep-Alive User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rep="http://www.dian.gov.co/servicios/facturaelectronica/ReportarFactura">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username><!--Removed--></wsse:Username>
<wsse:Password><!--Removed--></wsse:Password>
<wsse:NonceEncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">FmbZRkx1jh2A+imgjD2fLQ==</wsse:Nonce>
<wsu:Created>2015-10-06T12:00:33.762Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>

但是当我尝试生成它时,我只会得到它:

<s:Envelope xmln s:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<s:Header>
<o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username><!--Removed--></wsse:Username>
<wsse:Password><!--Removed--></wsse:Password>
<wsse: Nonce>t21C05VjRqomc+OehZoSzwqhmaM=</wsse:Nonce>
<wsse:Created>2018-08-28T08:03:40.749Z</wsse:Created>
</wsse:UsernameToken>
</o:Security>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<EnvioFacturaElectronicaPeticion xmlns="http://www.dian.gov.co/servicios/facturaelectronica/ReportarFactura">
<NIT><!--Removed--></NIT>
<InvoiceNumber>980000000</InvoiceNumber>
<IssueDate>2018-08-28T19:00:00</IssueDate>
<Document>UEsDBBQAAAAIAPBkHE3djKx2hwgAAEcdAAAeAAAAZmFjZV9mMDgxNjAwMjgzNDAwM0E2OTl.....

这就是我在做什么:

public string GeneraHeaderSOAP()
{
    const string apiUrl = "https://facturaelectronica.dian.gov.co/habilitacion/B2BIntegrationEngine/FacturaElectronica",
    apiUserName = "Username";

    string Password = "Password";
    string apiPassword = GetPassword(Password);

    EndpointAddress endpointAddress = new EndpointAddress(new Uri(apiUrl));

    SecurityBindingElement securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
    securityElement.AllowInsecureTransport = false;
    securityElement.EnableUnsecuredResponse = true;
    securityElement.IncludeTimestamp = false;

    TextMessageEncodingBindingElement encodingElement = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);
    HttpsTransportBindingElement transportElement = new HttpsTransportBindingElement();

    CustomBinding binding = new CustomBinding(securityElement, encodingElement, transportElement);

    //basic prueba
    BasicHttpSecurityMode securityMode = new BasicHttpSecurityMode();
    BasicHttpBinding binding2 = new BasicHttpBinding(securityMode);

    // Then assign username and password based on the proxy. For example -
    //var remoteAddress = new EndpointAddress("");
    WSDian.facturaElectronicaPortNameClient service = new WSDian.facturaElectronicaPortNameClient(binding, endpointAddress);
    //WSDian.facturaElectronicaPortNameClient service = new WSDian.facturaElectronicaPortNameClient(binding2, endpointAddress);
    service.ClientCredentials.UserName.UserName = apiUserName;
    service.ClientCredentials.UserName.Password = apiPassword;
    var Obtnonce = GetNonce();
    byte[] archivo = FileToByteArray(@"c:\temp\ws_f0816002834000000000A.zip");

    service.Open();

    WSDian.EnvioFacturaElectronica enviofactura = new WSDian.EnvioFacturaElectronica();

    enviofactura.NIT = "816006634";
    enviofactura.InvoiceNumber = "10";
    enviofactura.IssueDate = Convert.ToDateTime("2018-01-01 05:00:00");
    enviofactura.Document = archivo;

    //temporal

    WSDian.EnvioFacturaElectronicaPeticion envioFacturaElectronicaPeticion = new WSDian.EnvioFacturaElectronicaPeticion();
    WSDian.EnvioFacturaElectronicaRespuesta envioFacturaElectronicaRespuesta = new WSDian.EnvioFacturaElectronicaRespuesta();
    WSDian.AcuseRecibo acuseRecibo = new WSDian.AcuseRecibo();
    WSDian.ReceivedInvoice receivedInvoice = new WSDian.ReceivedInvoice();
    WSDian.facturaElectronicaPortNameClient facturaElectronicaPortNameClient = new WSDian.facturaElectronicaPortNameClient();

    envioFacturaElectronicaPeticion.EnvioFacturaElectronicaPeticion1 = enviofactura;
    var versionSoap = acuseRecibo.GetType();
    acuseRecibo = service.EnvioFacturaElectronica(envioFacturaElectronicaPeticion.EnvioFacturaElectronicaPeticion1);
}

我添加了对Web服务的引用,因此可以使用它,但是在调试代码时,出现此错误:

  

StatusCode = 400,StatusDescription =错误请求。

我无法根据规范生成SOAP请求,请帮忙。

谢谢。

0 个答案:

没有答案