Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the HttpWebRequest class

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.

like image 382
Developer Avatar asked Mar 12 '10 17:03

Developer


People also ask

Is HttpWebRequest obsolete?

Breaking change: WebRequest, WebClient, and ServicePoint are obsolete - . NET | Microsoft Learn. This browser is no longer supported. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

What is the use of HttpWebRequest in C#?

The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP. Do not use the HttpWebRequest constructor. Use the WebRequest.

What is the difference between HttpWebRequest and WebRequest?

WebRequest is an abstract class. The HttpWebRequest class allows you to programmatically make web requests to the HTTP server.

How do I get HttpWebRequest response?

The GetResponse method returns a WebResponse object that contains the response from the Internet resource. The actual instance returned is an HttpWebResponse, and can be typecast to that class to access HTTP-specific properties.


2 Answers

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
like image 77
Justin Avatar answered Oct 02 '22 18:10

Justin


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 service 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/">
   <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.

like image 25
Cheeso Avatar answered Oct 02 '22 17:10

Cheeso