views:

288

answers:

4

I instantiate the HttpWebRequest object:

HttpWebRequest httpWebRequest = 
    WebRequest.Create("http://game.stop.com/webservice/services/gameup")
    as HttpWebRequest;

When I "post" the data to this service, how does the service know which web method to submit the data to?

I do not have the code to this web service, all I know is that it was written in Java.

A: 

Different web services engines route incoming requests to particular web services implementations differently.

You said "web services", but didn't specify the use of SOAP. I'm going to assume SOAP.

The SOAP 1.1 specification says ...

The SOAPAction HTTP request header field can be used to indicate the intent of the SOAP HTTP request. The value is a URI identifying the intent. SOAP places no restrictions on the format or specificity of the URI or that it is resolvable. An HTTP client MUST use this header field when issuing a SOAP HTTP Request.

Most web services engines comply with the spec, and therefore use the SOAPAction: header. This obviously works only with SOAP-over-HTTP transmissions.

When HTTP is not used (say, TCP, or some other), the web services engine needs to fall back on something. Many use the message payload, specifically the name of the top-level element in the XML fragment within the soap:envelope. For example, the engine might look at this incoming message:

<soap:Envelope
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;
   <soap:Body>
       <m:GetAccountStatus xmlns:m="Some-URI">
           <acctnum>178263</acctnum>
       </m:GetAccountStatus>
   </soap:Body>
</soap:Envelope>

...find the GetAccountStatus element, and then route the request based on that.

Cheeso
A: 

If you're trying to talk to a Java web service, then you should not use HttpWebRequest. You should use "Add Service Reference" and point it to the Java service.

John Saunders
Adding a service reference is what i was doing, but the WSE security headers were not as per the liking of the java service, iam having to build out the headers manually, and so i am submitting the data using HttpWebRequest.I have tried using "Assertions" but that did not work for me (issues came up when building certain tags that are needed in the security header)
Nick
@Nick: WSE has nothing to do with "Add Service Reference". How is WSE involved? It's obsolete and shouldn't be used unless you have no other choices.
John Saunders
+1  A: 

This gets a bit complicated but it's perfectly doable.

You have to know the SOAPAction you want to take. If you don't you can't make the request. If you don't want to set this up manually you can add a service reference to Visual Studio but you will need to know the services endpoint.

The code below is for a manual SOAP request.

// load that XML that you want to post
// it doesn't have to load from an XML doc, this is just
// how we do it
XmlDocument doc = new XmlDocument();
doc.Load( Server.MapPath( "some_file.xml" ) );

// create the request to your URL
HttpWebRequest request = (HttpWebRequest)WebRequest.Create( Your URL );

// add the headers
// the SOAPACtion determines what action the web service should use
// YOU MUST KNOW THIS and SET IT HERE
request.Headers.Add( "SOAPAction", YOUR SOAP ACTION );

// set the request type
// we user utf-8 but set the content type here
request.ContentType = "text/xml;charset=\"utf-8\"";
request.Accept = "text/xml";
request.Method = "POST";

// add our body to the request
Stream stream = request.GetRequestStream();
doc.Save( stream );
stream.Close();

// get the response back
using( HttpWebResponse response = (HttpWebResponse)request.GetResponse() )
{
     // do something with the response here
}//end using
Justin
A: 

I am assuming its a REST based web service. here's some samples from Yahoo

http://developer.yahoo.com/dotnet/howto-rest_cs.html#get

JayD