Skip to main content
 首页 » 编程设计

c#之使用客户端 Web 服务时出现内容错误

2024年05月10日46wuhuacong

我在使用客户端 Web 服务时出现以下错误。

Error: The content type text/xml;charset=utf-8 of the response message does not match the content type of the binding (application/soap+msbin1+gzip). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly.

The first 807 bytes of the response were:

' soapenv:Client The request message must be sent using HTTP compression (RFC 1952 - GZIP). Please review the transmission instructions outlined in Section 5 of the AIR Submission Composition and Reference Guide located at https://www.irs.gov/for-Tax-Pros/Software-Developers/Information-Returns/Affordable-Care-Act-Information-Return-AIR-Program, correct any issues, and try again. TPE1112 '.==========================Error: System.Net.WebException: The remote server returned an error: (500) Internal Server Error. at System.Net.HttpWebRequest.GetResponse() at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

我知道有很多关于此错误的文章,但我无法获得解决方案或提示,如何解决此问题?

基本上客户提供了 WSDL 文件,我已将其作为“服务引用”添加到我的控制台应用程序中。

这是我的配置文件

<bindings> 
  <customBinding> 
    <binding name="BulkRequestTransmitterBinding"  > 
      <binaryMessageEncoding compressionFormat="GZip" /> 
      <httpsTransport/> 
    </binding> 
  </customBinding> 
</bindings> 
<client> 
  <endpoint address="MYENDPOINTURL" 
      binding="customBinding"  
      bindingConfiguration="BulkRequestTransmitterBinding" 
      contract="CONTRACTNAME"  
      name="BulkRequestTransmitterPort" /> 
</client> 

请您参考如下方法:

首先,您需要下载 this sample encoder from Microsoft

设置它并将其作为项目添加到您的解决方案中。之后您需要进行一些调整。

  1. 首先您需要将内容类型更改为“text/xml”而不是“application/x-gzip”。通过改变

    class GZipMessageEncoder : MessageEncoder 
    { 
    static string GZipContentType = "application/x-gzip"; 
    

    到

    static string GZipContentType = "text/xml"; 
    
  2. 在传输之前,您还需要更改内容编码 header ,this blog post describes it in more detail但代码在这里:

    using (new OperationContextScope(transmitter.InnerChannel)) 
    { 
    // Add a HTTP Header to an outgoing request 
    HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty(); 
    requestMessage.Headers["Content-Encoding"] = "gzip"; 
    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage; 
     
    var response = transmitter.BulkRequestTransmitter( 
        securityHeader, 
        security, 
        ref businessHeader, 
        transmitterManifestReqDtl, 
        transmitterType); 
    ParseSubmitResponse(response); 
    } 
    
  3. IRS 在要求您发送压缩的所有内容的同时,以未压缩的格式响应(至少发送器部分,谁知道他们决定如何处理状态部分)因此需要禁用解压缩例程,它位于

    ZipEncoder.CZipMessageEncoderFactory.GZipMessageEncoder.DecompressBuffer 
    

    你只需要注释掉using语句并交换

    int bytesRead = gzStream.Read(tempBuffer, 0, blockSize); 
    

    与

    int bytesRead = memoryStream.Read(tempBuffer, 0, blockSize); 
    
  4. 接下来,您需要更改内部消息编码消息版本以与 IRS 兼容。所以在

    GZipEncoder.GZipMessageEncodingElement.ApplyConfiguration 
    

    您需要将 TextMessageEncodingBindingElement 的构造函数更改为:

    binding.InnerMessageEncodingBindingElement = 
        new TextMessageEncodingBindingElement(MessageVersion.Soap11WSAddressing10, Encoding.UTF8); 
    
  5. 至此,你就差不多准备好了,你只需要在 app.config 中添加一些东西你需要添加以下部分(Service model 是标记以供引用的部分):

    <system.serviceModel> 
      <extensions> 
        <bindingElementExtensions> 
          <add name="gzipMessageEncoding" type="GZipEncoder.GZipMessageEncodingElement, GZipEncoder, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
        </bindingElementExtensions> 
      </extensions> 
    
  6. 现在您可以使绑定(bind)看起来像这样:

    <binding name="BulkRequestTransmitterBinding"> 
      <gzipMessageEncoding innerMessageEncoding="textMessageEncoding" /> 
      <httpsTransport /> 
    </binding> 
    

这样,您的代码应该成功传输到 IRS。