S
Sunny Henry via .NET 247
I'm new to VB .NET and I'm trying to home some skills on callingWeb Service methods from a VB .NET console application VIA HTTPGET, HTTP POST & SOAP. The HTTP POST works with no problem. However, the HTTP GET and SOAP messages don't give me thedesired result if any at all. The HTTP GET only writes theConsole.WriteLine("------------HTTP POST--------------") lineand nothing else. It also gives me the following error:
'DefaultDomain': Loaded'c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll', Nosymbols loaded.
'Tester': Loaded 'C:\Documents and Settings\Sunny Bear\MyDocuments\Visual Studio Projects\Tester\bin\Tester.exe', Symbolsloaded.
'Tester.exe': Loaded'c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll', No symbols loaded.
'Tester.exe': Loaded'c:\windows\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll', No symbols loaded.
The program '[2992] Tester.exe' has exited with code 0 (0x0).
The SOAP message only writes the
"Console.WriteLine("------------SOAP--------------")" line andbreaks on the line of code that states "If content.Length > 0Then" ..... It also gives the following error in the outputwindow:
'DefaultDomain': Loaded'c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll', Nosymbols loaded.
'Tester': Loaded 'C:\Documents and Settings\Sunny Bear\MyDocuments\Visual Studio Projects\Tester\bin\Tester.exe', Symbolsloaded.
'Tester.exe': Loaded'c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll', No symbols loaded.
'Tester.exe': Loaded'c:\windows\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll', No symbols loaded.
An unhandled exception of type 'System.NullReferenceException'occurred in Tester.exe
Additional information: Object reference not set to an instanceof an object.
'Tester.exe': Loaded'c:\windows\assembly\gac\microsoft.visualbasic\7.0.5000.0__b03f5f7f11d50a3a\microsoft.visualbasic.dll', No symbols loaded.
I'd appreciate really appreciate any help anyone has. I've beenstuck for about 3 days on this one. Here is my Consoleapplication code.....
Thannnnxxxx, Realmscape.....You can also reply (e-mail address removed)
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Collections.Specialized
Imports System.Diagnostics
Module Tester
Sub Main()
'Add to Main to enable tracing to the Console
Trace.Listeners.Add(New TextWriterTraceListener(Console.Out))
Trace.AutoFlush = True
''***** GET *****'
'Uses the GET METHOD to retrieve information from the WebService
Console.WriteLine("------------HTTP Get--------------")
Dim urlGet As String ="http://localhost/Woodgrove/Bank.asmx/GetAccount?intvendor=1"
GetData(urlGet, "", "GET", "")
'***** POST *****'
'Uses the POST METHOD to retrieve information from the WebService
Console.WriteLine("------------HTTP POST--------------")
Dim urlPost As String ="http://localhost/Woodgrove/Bank.asmx/GetAccount"
GetData(urlPost, "application/x-www-form-urlencoded", "POST","intvendor=1")
'urlstring 'content-type 'method 'variable
'***** SOAP *****'
'Uses the SOAP to retrieve information from the Web Service
Console.WriteLine("------------SOAP--------------")
Dim urlSOAP As String = "http://localhost/Woodgrove/Bank.asmx"
Dim strSoap As String = BuildSOAPMessage()
Dim strAction As String = "SOAPAction:""http://tempuri.org/GetAccount"""
GetData(urlSOAP, "text/xml; charset=utf-8", "POST", strSoap,strAction)
Console.ReadLine()
End Sub
Public Sub DisplayRequest(ByVal req As HttpWebRequest)
Trace.WriteLine("*** Request Start ***")
Trace.WriteLine(req.RequestUri.ToString())
DisplayHeaders(req.Headers)
Trace.WriteLine("*** Request End ***")
End Sub
Public Sub DisplayResponse(ByVal hresp As HttpWebResponse)
Trace.WriteLine(Nothing)
Trace.WriteLine("*** Response Start ***")
Trace.WriteLine(hresp.StatusCode)
Trace.WriteLine(hresp.StatusDescription)
DisplayHeaders(hresp.Headers)
DisplayContent(hresp)
Trace.WriteLine("*** Response End ***")
Trace.WriteLine(Nothing)
End Sub
Public Sub DisplayHeaders(ByVal headers As NameValueCollection)
Dim sItem As String
For Each sItem In headers
Trace.WriteLine(sItem & ": " & headers(sItem))
Next
End Sub
Public Sub DisplayContent(ByVal response As HttpWebResponse)
Dim strm As Stream = response.GetResponseStream()
If Not IsNothing(strm) Then
Dim sr As StreamReader = New StreamReader(strm, Encoding.ASCII)
Trace.WriteLine(sr.ReadToEnd())
End If
End Sub
Public Sub GetData(ByVal url As String, _
ByVal contentType As String, _
ByVal method As String, _
ByVal content As String, _
ByVal ParamArray headers() As String)
Dim req As HttpWebRequest = WebRequest.Create(url)
Dim header As String
Dim s As Stream
For Each header In headers
req.Headers.Add(header)
Next
If method.Length > 0 Then
req.Method = method
End If
If contentType.Length > 0 Then
req.ContentType = contentType
End If
If content.Length > 0 Then
req.ContentLength = content.Length
s = req.GetRequestStream
Dim sw As StreamWriter = New StreamWriter(s)
sw.Write(content)
sw.Close()
DisplayRequest(req)
Dim res As HttpWebResponse = req.GetResponse
DisplayResponse(res)
End If
End Sub
Public Function BuildSOAPMessage() As String
Dim st As MemoryStream = New MemoryStream(1024)
Dim result As String
Dim buffer() As Byte
Dim tr As New XmlTextWriter(st, Encoding.UTF8)
tr.WriteStartDocument()
tr.WriteStartElement("soap", "Envelope","http://schemas.xmlsoap.org/soap/envelope/")
tr.WriteAttributeString("xmlns", "xsi", Nothing,"http://www.w3.org/2001/XMLSchema-instance")
tr.WriteAttributeString("xmlns", "xsd", Nothing,"http://www.w3.org/2001/XMLSchema")
tr.WriteAttributeString("xmlns", "soap", Nothing,"http://schemas.xmlsoap.org/soap/envelope/")
tr.WriteStartElement("Body","http://schemas.xmlsoap.org/soap/envelope/")
'Lab 03 Exercise 4
'Insert code here
tr.WriteStartElement(Nothing, "GetAccount","http://tempuri.org")
tr.WriteEndElement()
tr.WriteEndElement()
tr.WriteEndDocument()
tr.Flush()
buffer = st.GetBuffer()
Dim d As Decoder = Encoding.UTF8.GetDecoder()
Dim chars() As Char
ReDim chars(buffer.Length)
d.GetChars(buffer, 2, buffer.Length - 2, chars, 0)
tr.Close()
st.Close()
BuildSOAPMessage = result
End Function
End Module
'DefaultDomain': Loaded'c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll', Nosymbols loaded.
'Tester': Loaded 'C:\Documents and Settings\Sunny Bear\MyDocuments\Visual Studio Projects\Tester\bin\Tester.exe', Symbolsloaded.
'Tester.exe': Loaded'c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll', No symbols loaded.
'Tester.exe': Loaded'c:\windows\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll', No symbols loaded.
The program '[2992] Tester.exe' has exited with code 0 (0x0).
The SOAP message only writes the
"Console.WriteLine("------------SOAP--------------")" line andbreaks on the line of code that states "If content.Length > 0Then" ..... It also gives the following error in the outputwindow:
'DefaultDomain': Loaded'c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll', Nosymbols loaded.
'Tester': Loaded 'C:\Documents and Settings\Sunny Bear\MyDocuments\Visual Studio Projects\Tester\bin\Tester.exe', Symbolsloaded.
'Tester.exe': Loaded'c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll', No symbols loaded.
'Tester.exe': Loaded'c:\windows\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll', No symbols loaded.
An unhandled exception of type 'System.NullReferenceException'occurred in Tester.exe
Additional information: Object reference not set to an instanceof an object.
'Tester.exe': Loaded'c:\windows\assembly\gac\microsoft.visualbasic\7.0.5000.0__b03f5f7f11d50a3a\microsoft.visualbasic.dll', No symbols loaded.
I'd appreciate really appreciate any help anyone has. I've beenstuck for about 3 days on this one. Here is my Consoleapplication code.....
Thannnnxxxx, Realmscape.....You can also reply (e-mail address removed)
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Collections.Specialized
Imports System.Diagnostics
Module Tester
Sub Main()
'Add to Main to enable tracing to the Console
Trace.Listeners.Add(New TextWriterTraceListener(Console.Out))
Trace.AutoFlush = True
''***** GET *****'
'Uses the GET METHOD to retrieve information from the WebService
Console.WriteLine("------------HTTP Get--------------")
Dim urlGet As String ="http://localhost/Woodgrove/Bank.asmx/GetAccount?intvendor=1"
GetData(urlGet, "", "GET", "")
'***** POST *****'
'Uses the POST METHOD to retrieve information from the WebService
Console.WriteLine("------------HTTP POST--------------")
Dim urlPost As String ="http://localhost/Woodgrove/Bank.asmx/GetAccount"
GetData(urlPost, "application/x-www-form-urlencoded", "POST","intvendor=1")
'urlstring 'content-type 'method 'variable
'***** SOAP *****'
'Uses the SOAP to retrieve information from the Web Service
Console.WriteLine("------------SOAP--------------")
Dim urlSOAP As String = "http://localhost/Woodgrove/Bank.asmx"
Dim strSoap As String = BuildSOAPMessage()
Dim strAction As String = "SOAPAction:""http://tempuri.org/GetAccount"""
GetData(urlSOAP, "text/xml; charset=utf-8", "POST", strSoap,strAction)
Console.ReadLine()
End Sub
Public Sub DisplayRequest(ByVal req As HttpWebRequest)
Trace.WriteLine("*** Request Start ***")
Trace.WriteLine(req.RequestUri.ToString())
DisplayHeaders(req.Headers)
Trace.WriteLine("*** Request End ***")
End Sub
Public Sub DisplayResponse(ByVal hresp As HttpWebResponse)
Trace.WriteLine(Nothing)
Trace.WriteLine("*** Response Start ***")
Trace.WriteLine(hresp.StatusCode)
Trace.WriteLine(hresp.StatusDescription)
DisplayHeaders(hresp.Headers)
DisplayContent(hresp)
Trace.WriteLine("*** Response End ***")
Trace.WriteLine(Nothing)
End Sub
Public Sub DisplayHeaders(ByVal headers As NameValueCollection)
Dim sItem As String
For Each sItem In headers
Trace.WriteLine(sItem & ": " & headers(sItem))
Next
End Sub
Public Sub DisplayContent(ByVal response As HttpWebResponse)
Dim strm As Stream = response.GetResponseStream()
If Not IsNothing(strm) Then
Dim sr As StreamReader = New StreamReader(strm, Encoding.ASCII)
Trace.WriteLine(sr.ReadToEnd())
End If
End Sub
Public Sub GetData(ByVal url As String, _
ByVal contentType As String, _
ByVal method As String, _
ByVal content As String, _
ByVal ParamArray headers() As String)
Dim req As HttpWebRequest = WebRequest.Create(url)
Dim header As String
Dim s As Stream
For Each header In headers
req.Headers.Add(header)
Next
If method.Length > 0 Then
req.Method = method
End If
If contentType.Length > 0 Then
req.ContentType = contentType
End If
If content.Length > 0 Then
req.ContentLength = content.Length
s = req.GetRequestStream
Dim sw As StreamWriter = New StreamWriter(s)
sw.Write(content)
sw.Close()
DisplayRequest(req)
Dim res As HttpWebResponse = req.GetResponse
DisplayResponse(res)
End If
End Sub
Public Function BuildSOAPMessage() As String
Dim st As MemoryStream = New MemoryStream(1024)
Dim result As String
Dim buffer() As Byte
Dim tr As New XmlTextWriter(st, Encoding.UTF8)
tr.WriteStartDocument()
tr.WriteStartElement("soap", "Envelope","http://schemas.xmlsoap.org/soap/envelope/")
tr.WriteAttributeString("xmlns", "xsi", Nothing,"http://www.w3.org/2001/XMLSchema-instance")
tr.WriteAttributeString("xmlns", "xsd", Nothing,"http://www.w3.org/2001/XMLSchema")
tr.WriteAttributeString("xmlns", "soap", Nothing,"http://schemas.xmlsoap.org/soap/envelope/")
tr.WriteStartElement("Body","http://schemas.xmlsoap.org/soap/envelope/")
'Lab 03 Exercise 4
'Insert code here
tr.WriteStartElement(Nothing, "GetAccount","http://tempuri.org")
tr.WriteEndElement()
tr.WriteEndElement()
tr.WriteEndDocument()
tr.Flush()
buffer = st.GetBuffer()
Dim d As Decoder = Encoding.UTF8.GetDecoder()
Dim chars() As Char
ReDim chars(buffer.Length)
d.GetChars(buffer, 2, buffer.Length - 2, chars, 0)
tr.Close()
st.Close()
BuildSOAPMessage = result
End Function
End Module