I'm new to object oriented programming so I just need help on the basics here. I have an xml string that I've tested in a separate program and is able to communicate with my soap server correctly. I'm trying to add a request in Visual Basic 2008 to essentially take the soap string, send it as an http POST, and display the xml that is returned. I basically have every part figured out (I think) except I don't know how to apply the actual SOAP string to the request.
Below is a sample of what I'm doing, I realize it's not the correct way to work with XML as it's totally unscalable but I just need to get the basics working for now and I will code it correctly later. (I'm in a time crunch and don't have enough .net experience to do it right yet)
'dim soap request strings
Dim TestEndPoint As String
TestEndPoint = "http://SoapEndPoint/bla/bla/bla"
Dim SOAPRqst As String
SOAPRqst = "comfirmed_Working_Soap_String_Goes_Here"
' create the request object
Dim wR As WebRequest = WebRequest.Create(TestEndPoint)
' Get the response.
Dim response As HttpWebResponse = CType(wR.GetResponse(), HttpWebResponse)
' Display the status.
MsgBox(response.StatusDescription)
' Get the stream containing content returned by the server.
Dim dataStream As Stream = response.GetResponseStream()
' Open the stream us开发者_StackOverflow中文版ing a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
MsgBox(responseFromServer)
' Cleanup the streams and the response.
reader.Close()
dataStream.Close()
response.Close()
Using .net, we generally don't handle SOAP envelopes that way, but through web services (or more recently using WCF).
If that server exposes a WSDL contract in that address, you should try to add a Web Reference
into your project and to manipulate that services using a automatically generate proxy class.
You need to use WebRequest.GetRequestStream
if you're going to do it this way.
If you really need simplicity, then you should use WebClient
.
Note also:
- You should never manipulate XML as a string. This will break as soon as you need to manipulate XML that has illegal (for XML) characters in it. You should use one of the XML APIs instead. Since you're using VB.NET, if you're also using VS2008, you get to use XML Literals, to create real XML inline.
Several of the objects you're creating implement the
IDisposable
interface. This means they should be created and accessed from aUsing
block. For instance:Using dataStream As Stream = response.GetResponseStream() ' ... End Using
This will ensure that the
Dispose
method is called, even if an exception is thrown from within the block. This is especially important for objects like streams, which have unmanaged resources that need to be cleaned up.
BTW, if you're just getting started, I have no idea why you want to start off by doing things the hard way. The easy way to work with web services is to just use "Add Service Reference" to point to the WSDL file of the service. That will produce a class you can use to call the web service just as though it were a normal class. It will also produce classes for any complex parameters or return values used by the service. All you'll need to do is create objects, fill them in, then call the service and get the result as objects. No need to screw around with XML.
精彩评论