Using the HttpWebRequest class

前端 未结 3 589
梦如初夏
梦如初夏 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: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
    

提交回复
热议问题