Using the HttpWebRequest class

前端 未结 3 561
梦如初夏
梦如初夏 2020-12-10 12:32

I instantiate the HttpWebRequest object:

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


        
相关标签:
3条回答
  • 2020-12-10 13:02

    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.

    0 讨论(0)
  • 2020-12-10 13:07

    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
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题