使用HttpWebRequest类

我实例化HttpWebRequest对象:

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

当我将数据“发布”到此服务时,服务如何知道将数据提交到哪个Web方法?

我没有这个Web服务的代码,我所知道的是它是用Java编写的。

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

您必须知道要采用的SOAPAction。 如果你不这样做,你就无法提出要求。 如果您不想手动设置它,可以向Visual Studio添加服务引用,但您需要知道服务端点。

下面的代码用于手动SOAP请求。

 // 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 

不同的Web服务引擎以不同方式将传入请求路由到特定Web服务实现

您说“Web服务”,但没有指定使用SOAP。 我将假设SOAP。

SOAP 1.1规范说……

SOAPAction HTTP请求头字段可用于指示SOAP HTTP请求的意图。 该值是标识intent的URI。 SOAP对URI的格式或特性没有限制,也没有可解析的限制。 发出SOAP HTTP请求时,HTTP客户端必须使用此头字段。

大多数Web服务引擎都符合规范,因此使用SOAPAction:标头。 这显然只适用于SOAP-over-HTTP传输。

当不使用HTTP(比如TCP或其他)时,Web服务引擎需要依赖某些东西。 许多人使用消息有效负载,特别是soap:envelope XML片段中顶级元素的名称。 例如,引擎可能会查看此传入消息:

    178263    

…找到GetAccountStatus元素,然后根据该元素路由请求。

如果您正在尝试与Java Web服务进行通信,那么您不应该使用HttpWebRequest。 您应该使用“添加服务引用”并将其指向Java服务。