使用HttpWebRequest类HttpWebRequest

2023-09-02 11:58:45 作者:淺藍Se的天空

我实例化HttpWebRequest对象:

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?

我没有code此Web服务,我所知道的是,它是用Java编写的。

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

推荐答案

这变得有点复杂,但它是完全可行的。

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

您必须知道你想坐的SOAPAction。如果不这样做,你不能提出这项要求。如果你不想手动设置这个,你就可以添加一个服务引用到Visual Studio,但你需要知道服务端点。

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.

在code以下是手动SOAP请求。

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