Вопрос

I have inherited a code that has worked for some time in our company already, and now I'm clueless as to what exactly is wrong with it. Without further ado, let's get to the issue.

How does it work

We have a system of communication set as following: JavaScript <-Ajax-> Web Services Proxy <-> Web Services Basically, JavaScript sends which service to call, which method with which arguments to the proxy, proxy then constructs Http Request, and that is then sent to one of our Web Services.

What's the problem?

The new thing that's happening now is that I am trying to attach a SOAP header to the Http request. Rigt now, the JavaScript code is irrelevant - I'm fairly sure the issue lies somewhere between the proxy and the web services. The code constructing the HTTP request looks like this:

HttpWebRequest basicRequest = (HttpWebRequest)WebRequest.Create(this.url);
basicRequest.ContentType = "application/json; charset=\"utf-8\""; 
basicRequest.Method = "POST";
basicRequest.AllowWriteStreamBuffering = false;

if (Ticket != string.Empty && Ticket != null)
{
    basicRequest.Headers["UserAuthenticationHeader"] = Ticket;
}

Now when I check the request before calling the

this.HttpResponse = (HttpWebResponse)HttpRequest.GetResponse();

method, debugger tells me the header is there. To be more precise, these are the contents of Headers attribute:

{Content-Type: application/json; charset="utf-8"
 UserAuthenticationHeader: user;ticket
 Host: localhost:25691
 Content-Length: 55
 Expect: 100-continue}

Now, the appropriate method in the web services just got called. Now I would expect that this code:

public class UserAuthenticationHeader : SoapHeader
{
    public string Ticket { get; set; }
}

[WebService(Namespace = "http:*****", Description = "")]
[System.Web.Script.Services.ScriptService]
public class DataGetService : System.Web.Services.WebService
{
    public virtual UserAuthenticationHeader UserAuthenticationHeader { get; set; }

    [WebMethod(Description = "Gets all modules")]
    [SoapHeader("UserAuthenticationHeader")]
    public ModulesDS GetMetadataModules(Guid GlobalUserId)
    {
       Authentication.Authenticate(UserAuthenticationHeader);

       return Sql.GetMetadataModules(GlobalUserId);
    }
}

is going to fill the contents of UserAuthenticationHeader, based on a couple of MSDN articles, that this would insert the value of "UserAuthenticationHeader" into UserAuthenticationHeader property. But it doesn't. When I get here, it's null. So yeah, what am I doing wrong? I've been struggling with this issue for past 5 hours and just can't figure out anything. I'm generally unable to transfer any headers whatsoever, and I have even tried to parse trough SoapUnknownHeader to no avail.

I have tried following a couple of tutorials on how to send over soap headers, but I mostly just wrote the same code that was already present in the application. I tried different ways of inserting the headers into HttpWebRequest, but all just basically do the same thing (not surprising). Other than that, I've not been able to find any good sources on this particular communication, so I'm just stuck.

Это было полезно?

Решение

Agreeing with @john-saunders. http://msdn.microsoft.com/en-us/library/8728chd5%28v=vs.100%29.aspx is a great example of what SOAP headers look like. Notice that they are serialized inside of the SOAP XML payload:

<soap:Header>
    <UserAuthenticationHeader>
        <user>uservalue</user>
        <ticket>tickevalue</ticket>
    </UserAuthenticationHeader>
</soap:Header>

There are tools to add elements to a SOAP message using WCF.Message or if you're using a service proxy. HttpWebRequest doesn't know anything about the SOAP message content, so you can always inject the XML directly into the request.

Другие советы

SOAP headers do not reside in the HTTP headers. They reside in the SOAP message itself.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top