soap envelope using C# problem

Posted on 2014-07-11
Last Modified: 2016-02-26
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";

        private string BuildSoapHeader()
            StringBuilder retValue = new StringBuilder("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"\" ");
            retValue.Append("xmlns:SOAP-ENC=\"\" ");
            retValue.Append("xmlns:xsi=\"\" ");
            retValue.Append("xmlns:xsd=\"\"> ");
            retValue.Append("<m:PI00WEBSOperationRequest_header xmlns:m=\"\">");
            retValue.Append("<m:DateTime>" + DateTime.Now.ToString() + "</m:DateTime>");
            retValue.Append("<m:uuid>" + this._fiserveUUID +"</m:uuid>");
            retValue.Append("<m:PrincipalPWD>" + this._fiservePrincipalPWD + "</m:PrincipalPWD>");
            retValue.Append("<m:PrincipalID>"+ this._fiservePrincipalID +"</m:PrincipalID>");
            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:URI>" + this._fiserveURI + "</m:URI>");
            return retValue.ToString();
Question by:Miles Thornton
    LVL 21

    Expert Comment

    by:Randy Poole
    can you give me a printout of the actual retValue
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    When I use the Immediate Window to examine the output of BuildSoapHeader(), I still see the escape sequence "\" literal
    And you will. That's just how the Immediate Window works. See:

    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Note that you do not add a SOAP header to the HTTP request headers. It needs to go in the request body, just like the envelope body does. It's all a part of the same envelope, and that envelope needs to appear in the HTTP request body. You need to open the request stream in order to write to the request body.
    LVL 1

    Author Comment

    by:Miles Thornton
    I am SO confused... Thank you all for your help!
    I've attached the class file for your perusal.

    Kaufmed, what goes in the HTTP Request header?

    Clearly, I'm doing this wrong... I'd really appreciate some detailed advice.

    Randy Poole, you asked, so this is what I have: "SOAP: <SOAP-ENV:Envelope xmlns:SOAP-ENV=\"\" xmlns:SOAP-ENC=\"\" xmlns:xsi=\"\" xmlns:xsd=\"\"> <SOAP-ENV:Header><m:PI00WEBSOperationRequest_header xmlns:m=\"\"><m:LSPHeader><m:Service><m:DateTime>7/12/2014 11:14:58 AM</m:DateTime><m:uuid>aaaaaaa</m:uuid></m:Service><m:Security><m:AuthenticationMaterial><m:PrincipalPWD>aaaaaaaaa</m:PrincipalPWD><m:PrincipalID>aaaaaa</m:PrincipalID></m:AuthenticationMaterial></m:Security><m:Client><m:VendorID>aaaaaa</m:VendorID><m:AppID>aaaaaa</m:AppID><m:OrgID>aaaaaaaa</m:OrgID><m:SessionID>aaa</m:SessionID></m:Client><m:DataSource><m:URI></m:URI></m:DataSource></m:LSPHeader></m:PI00WEBSOperationRequest_header></SOAP-ENV:Header></SOAP-ENV:Envelope>"
    LVL 1

    Author Comment

    by:Miles Thornton
    The Sample Header I was given:
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="" xmlns:SOAP-ENC="" xmlns:xsi="" xmlns:xsd="">
    <m:PI00WEBSOperationRequest_header xmlns:m="">
    <m:AppID>TQAL client</m:AppID>
    <SOAP-ENV:Body>refer to specific message body</SOAP-ENV:Body>

    The Sample Body I was given:
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="" xmlns:SOAP-ENC="" xmlns:xsi="" xmlns:xsd="">
    <m:PI00WEBSOperationRequest_header xmlns:m="">
    <m:PI00WEBSOperation xmlns:m="">
    LVL 1

    Accepted Solution

    It appears that all I needed to do was add: "SOAP: " before the "<SOAP-ENV:Envelo..." part to comply with the Key: Value pair restriction and the header added just fine.

    Thanks all!
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    I haven't done raw SOAP before, so I'm unfamiliar with the spec (I usually let WCF do all of the heavy lifting for me), but as I recall there should only be two headers that vary when using SOAP. One is the Content-Type, and the other is SOAPAction, I believe. Your Content-Type appears to be OK, so I think you need to only add in the SOAPAction.


    httpRequest.Headers.Add("SOAPAction", "http://server:port/WebServiceMethodName");

    Open in new window

    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Do let us know how it goes adding that to the HTTP headers. I'd strongly advise you to read up on SOAP specification because you have a fundamental misunderstanding of how it works.

    Good luck.
    LVL 1

    Author Closing Comment

    by:Miles Thornton
    I applaud the Experts for their time and efforts on my behalf. I'd like to award each of those who responded with points, as I feel they deserve something, some take-away even though I got my answer through and we weren't even close here...


    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Conditional Formatting 4 22
    DNS records transfer 3 62
    Technology stack suggestions 4 53
    Error handling in PHP 5 39
    A Change in PHP Behavior with Session Write Short Circuit ( (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
    Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
    This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now