【问题标题】:"Specified value has invalid HTTP Header characters. Parameter name: name"“指定的值包含无效的 HTTP 标头字符。参数名称:名称”
【发布时间】:2021-03-10 23:20:47
【问题描述】:

我正在尝试使用 C# 构建一个肥皂信封;我收到错误消息:“指定的值包含无效的 HTTP 标头字符。参数名称:名称”添加标头时。 当我使用即时窗口检查 BuildSoapHeader() 的输出时,我仍然看到转义序列“\”文字,我想知道这是问题所在还是其他原因。

请帮忙!

WebRequest webRequest = WebRequest.Create(this._fiserveURI);
HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
httpRequest.Method = "POST";
httpRequest.ContentType = "text/xml; charset=utf-8";
httpRequest.Headers.Add(this.BuildSoapHeader());
...

    private string BuildSoapHeader()
    {
        StringBuilder retValue = new StringBuilder("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" ");
        retValue.Append("xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" ");
        retValue.Append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
        retValue.Append("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> ");
        retValue.Append("<SOAP-ENV:Header>");
        retValue.Append("<m:PI00WEBSOperationRequest_header xmlns:m=\"http://www.FiservLSP.RequestHeader.com\">");
        retValue.Append("<m:LSPHeader>");
        retValue.Append("<m:Service>");
        retValue.Append("<m:DateTime>" + DateTime.Now.ToString() + "</m:DateTime>");
        retValue.Append("<m:uuid>" + this._fiserveUUID +"</m:uuid>");
        retValue.Append("</m:Service>");
        retValue.Append("<m:Security>");
        retValue.Append("<m:AuthenticationMaterial>");
        retValue.Append("<m:PrincipalPWD>" + this._fiservePrincipalPWD + "</m:PrincipalPWD>");
        retValue.Append("<m:PrincipalID>"+ this._fiservePrincipalID +"</m:PrincipalID>");
        retValue.Append("</m:AuthenticationMaterial>");
        retValue.Append("</m:Security>");
        retValue.Append("<m:Client>");
        retValue.Append("<m:VendorID>" + this._fiserveVendorID + "</m:VendorID>");
        retValue.Append("<m:AppID>" + this._fiserveAppID + "</m:AppID>");
        retValue.Append("<m:OrgID>" + this._fiserveOrgID + "</m:OrgID>");
        retValue.Append("<m:SessionID>" + this._fiserveSessionID + "</m:SessionID>");
        retValue.Append("</m:Client>");
        retValue.Append("<m:DataSource>");
        retValue.Append("<m:URI>" + this._fiserveURI + "</m:URI>");
        retValue.Append("</m:DataSource>");
        retValue.Append("</m:LSPHeader>");
        retValue.Append("</m:PI00WEBSOperationRequest_header>");
        retValue.Append("</SOAP-ENV:Header>");
        //retValue.Append("<SOAP-ENV:Body></SOAP-ENV:Body>");
        retValue.Append("</SOAP-ENV:Envelope>");
        return retValue.ToString();
    }

【问题讨论】:

    标签: c# soap


    【解决方案1】:

    您的字符串可能应该采用HeaderName: Value 的格式,所以在您的情况下,它应该类似于SOAP: &lt;SOAP-ENV:Envelo...

    但是,您很少会在 HTTP 标头中看到这样的字符串;您通常会在 POST 正文中看到它。如果目标服务真的想要这些数据在标头中,它可能应该首先是 Base64 编码的。

    【讨论】:

    • 看来您的建议 (SOAP:
    【解决方案2】:

    对我来说,这是我命名的 Header。它有一个无效字符(空格)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-04
      • 2017-12-04
      • 2020-02-26
      • 2014-12-04
      • 1970-01-01
      • 2017-03-18
      相关资源
      最近更新 更多