我实例化了HttpWebRequest对象:
HttpWebRequest httpWebRequest =
WebRequest.Create("http://game.stop.com/webservice/services/gameup")
as HttpWebRequest;
当我向这个服务“post”数据时,服务是如何知道将数据提交到哪个Web方法的?
我没有这个Web服务的代码,我只知道它是用Java编写的。我实例化了HttpWebRequest对象:
HttpWebRequest httpWebRequest =
WebRequest.Create("http://game.stop.com/webservice/services/gameup")
as HttpWebRequest;
当我向这个服务“post”数据时,服务是如何知道将数据提交到哪个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
<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>
GetAccountStatus
元素,然后根据其路由请求。
using( HttpWebResponse response = (HttpWebResponse)request.GetResponse() )
来获取响应时,出现了错误。在那一行上。有没有其他方法可以获取响应?它说GetResponse()
不被识别。 - Solomon Closson