“Specified value has invalid HTTP Header characters. Parameter name: name”

I’m trying to build a soap envelope using C#; I’m getting the error: “Specified value has invalid HTTP Header characters. Parameter name: name” when adding the header. When I use the Immediate Window to examine the output of BuildSoapHeader(), I still see the escape sequence “” literal and I wonder if that’s the issue or if it’s something else.

Please help!

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();
    }

Answer

Your string is probably supposed to be in the format HeaderName: Value so in your case it’d be something like SOAP: <SOAP-ENV:Envelo...

However, you’d rarely see a string like this within a HTTP header; you’d typically see it in a POST body. If the target service really wants this data in the headers, it’s probably supposed to be Base64-encoded first.

Leave a Reply

Your email address will not be published. Required fields are marked *