|
Designing a Generic Soap Client Using Visual Basic.Net, by Chandu Thota
WSJ Vol 02 Issue 08 - pg.14
Listing 1
POST /StockQuote HTTP/1.1
Host: www.stockquoteserver.com
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
SOAPAction: "Some-URI"
<Envelope
xmlns="http://schemas.xmlsoap.org/soap/envelope/"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<Body>
<m:GetLastTradePrice xmlns:m="Some-URI">
<symbol>DIS
</m:GetLastTradePrice>
</Body>
</Envelope>
Listing 2
Public Sub SetHeader(ByVal Name As String, ByVal Value As String)
Select Case (Name)
Case "Method"
objHTTPReq.Method = Value
Case "Accept"
objHTTPReq.Accept = Value
Case Else
objHTTPReq.Headers.Add(Name, Value)
End Select
End Sub
Listing 3
Public Function BuildEnv(ByVal Body As String) As String
BuildEnv = "" & _
"<Envelope xmlns='http://schemas.xmlsoap.org/soap/
envelope/'>" & _
"<Body>" & Body & "</Body></Envelope>"
End Function
Listing 4
Public Sub SetCred(ByVal UName As String, ByVal UPass
As String)
Dim objCred As New Net.NetworkCredential(UName, UPass)
objHTTPReq.Credentials = objCred
objCred = Nothing
End Sub
Listing 5
Public Sub Send(ByVal Message As String)
Dim objStream As System.IO.StreamWriter
Try
objStream = New StreamWriter(objHTTPReq.GetRequestStream(), Encoding.UTF8)
objStream.Write(Message)
objStream.Close()
Catch
End Try
objStream = Nothing
End Sub
Listing 6
Public Function GetResponse() As String
Dim objXML As New System.Xml.XmlDocument()
Try
objHTTPRes = objHTTPReq.GetResponse()
objXML.Load(objHTTPRes.GetResponseStream())
GetResponse = objXML.DocumentElement.FirstChild.InnerXml.ToString
Catch e As WebException
objHTTPRes = e.Response
objXML.Load(objHTTPRes.GetResponseStream())
GetResponse = objXML.OuterXml.ToString
End Try
objXML = Nothing
End Function
|