Resending: Problems trying to build basic web service with client for testing

B

Brent White

(RESENDING: This article did not appear on the newsgroup)

The documentation provided by the people here has been somewhat
helpful, but it appears I am missing the boat somewhere.

I have a web service I designed in VS 2005 that right now is doing
nothing more than returning an XML stream to a client. Eventually, it
will post an HTTP request to an outside server, but I'm trying to
learn VS 2005 as I go so I'm right now trying just to get a web
service to post to and it posts back what you post to it through
SOAP. This is the code:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

<System.Web.Services.WebService(Namespace:="http://badger.com/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
_
<ToolboxItem(False)> _
Public Class Service1
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function AuthorizeAPI(ByVal Input1 As InformationToPass) As
ReturnParams
Dim rp As New ReturnParams
'Return "Hi, this page is served by Brent White's own system
and can be looked at " & Input1.SecondParam & " times."
'Return Input1.SerialNumber
'Return Input1.Dev_SerialNumber
rp.SJName = Input1.SJName
rp.Email = Input1.Email
'Return Input1.BillStreet
'Return Input1.BillStreet2
'Return Input1.BillStreet3
'Return Input1.BillStreet4
'Return Input1.BillCity
'Return Input1.BillState
'Return Input1.BillZip
'Return Input1.BillCountry
'Return Input1.BillPhone
'Return Input1.BillFax
'Return Input1.ShipName
'Return Input1.ShipStreet
'Return Input1.ShipStreet2
'Return Input1.ShipStreet3
'Return Input1.ShipStreet4
'Return Input1.ShipCity
'Return Input1.ShipState
'Return Input1.ShipZip
'Return Input1.ShipCountry
'Return Input1.ShipPhone
'Return Input1.ShipFax
'Return Input1.AccountNumber
'Return Input1.MonthExpr
'Return Input1.YearExpr
'Return Input1.CVV2
'Return Input1.TransactionAmount
'Return Input1.SalesTax
'Return Input1.CustomerCode
'Return Input1.PONumber
'Return Input1.ShippingAmount
'Return Input1.SummaryCommodityCode
'Return Input1.OrderString_lvl3
Return rp
End Function

End Class

Public Class InformationToPass
Public SerialNumber As String = "000399239784"
Public Dev_SerialNumber As String = "100008901765"
Public SJName As String
Public Email As String
Public BillStreet As String
Public BillStreet2 As String
Public BillStreet3 As String
Public BillStreet4 As String
Public BillCity As String
Public BillState As String
Public BillZip As String
Public BillCountry As String
Public BillPhone As String
Public BillFax As String
Public ShipName As String
Public ShipStreet As String
Public ShipStreet2 As String
Public ShipStreet3 As String
Public ShipStreet4 As String
Public ShipCity As String
Public ShipState As String
Public ShipZip As String
Public ShipCountry As String
Public ShipPhone As String
Public ShipFax As String
Public OrderNumber As String
Public AccountNumber As String
Public MonthExpr As String
Public YearExpr As String
Public CVV2 As String
Public TransactionAmount As String
Public SalesTax As String
Public CustomerCode As String
Public PONumber As String
Public ShippingAmount As String
Public SummaryCommodityCode As String
Public OrderString_lvl3 As String
End Class

Public Class ReturnParams
Public SJName As String
Public Email As String
End Class


web.config:

<?xml version="1.0"?>

<configuration>

<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Testing.My.MySettings"
type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</sectionGroup>
</configSections>
<appSettings/>
<connectionStrings/>

<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.

Visual Basic options:
Set strict="true" to disallow all data type conversions
where data loss can occur.
Set explicit="true" to force declaration of all variables.
-->
<compilation debug="true" strict="false" explicit="true" />
<pages>
<namespaces>
<clear />
<add namespace="System" />
<add namespace="System.Collections" />
<add namespace="System.Collections.Specialized" />
<add namespace="System.Configuration" />
<add namespace="System.Text" />
<add namespace="System.Text.RegularExpressions" />
<add namespace="System.Web" />
<add namespace="System.Web.Caching" />
<add namespace="System.Web.SessionState" />
<add namespace="System.Web.Security" />
<add namespace="System.Web.Profile" />
<add namespace="System.Web.UI" />
<add namespace="System.Web.UI.WebControls" />
<add namespace="System.Web.UI.WebControls.WebParts" />
<add namespace="System.Web.UI.HtmlControls" />
</namespaces>
</pages>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly"
defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
<add name="HttpSoap"/>
</protocols>
</webServices>

</system.web>
<applicationSettings>
<Testing.My.MySettings>
<setting name="Testing_localhost_Service1"
serializeAs="String">
<value>http://localhost/Service1.asmx</value>
</setting>
</Testing.My.MySettings>
</applicationSettings>
</configuration>


And this is the VB code that forms the client:

Imports System.IO
Imports System.Xml

Module Module1

Sub Main()
Dim msg As String
Dim strm As Stream
Dim strmrdr As System.IO.StreamReader
Dim http As New MSXML2.XMLHTTP30
Dim serviceurl As String = "http://localhost/Service1.asmx"
msg = "<soap:Envelope "
msg = msg & "xmlns:xsi='http://www.w3.org/2001/XMLSchema-
instance'"
msg = msg & "xmlns:xsi='http://www.w3.org/2001/XMLSchema'"
msg = msg & "xmlns:soap='http://schemas.xmlsoap.org/soap/
envelope'>"
msg = msg & "<soap:Body>"
msg = msg & "<AuthorizeAPI xmlns='http://badger.com'>"
msg = msg & "<Input1>"
msg = msg & "<SJName>Me</SJName>"
msg = msg & "<Email>[email protected]</Email>"
msg = msg & "</Input1>"
msg = msg & "</AuthorizeAPI>"
msg = msg & "</Soap:Body>"
msg = msg & "</Soap:Envelope>"

http.open("POST", serviceurl, False)
http.setRequestHeader("SOAPACTION", """http://badger.com/
AuthorizeAPI""")
http.setRequestHeader("Content-Type", "text/xml")
http.send(msg)
Console.Write(http.responseStream.ToString)
strm = http.responseStream
strmrdr = New StreamReader(strm)
'Dim xmlreader As New XmlTextReader(strm)
'Do While (xmlreader.Read())
' Select Case XmlReader.NodeType
' Case XmlNodeType.Element
' Console.Write("<" + XmlReader.Name)
' If XmlReader.HasAttributes Then 'If attributes exist
' While XmlReader.MoveToNextAttribute()
' 'Display attribute name and value.
' Console.Write(" {0}='{1}'", XmlReader.Name, XmlReader.Value)
'End While
'End If
'Console.WriteLine(">")
' Case XmlNodeType.Text 'Display the text in each element.
'Console.WriteLine(XmlReader.Value)
' Case XmlNodeType.EndElement 'Display end of element.
'Console.Write("</" + XmlReader.Name)
'Console.WriteLine(">")
'End Select
'Loop
' MsgBox(msg)
'Console.Write(msg)
Console.ReadKey()

End Sub

End Module


First, I get a message on the console: System.__ComObject

And I get a runtime error:

Unable to cast COM object of type 'System.__ComObject' to class type
'System.IO.Stream'. Instances of types that represent COM components
cannot be cast to types that do not represent COM components; however
they can be cast to interfaces as long as the underlying COM component
supports QueryInterface calls for the IID of the interface.


What I want to do is pull the response into a stream reader so I can
make sure what I'm getting from the web service is what I am expecting
before I start calling another web service from my web service.

Can someone help me
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Brent said:
(RESENDING: This article did not appear on the newsgroup)

The documentation provided by the people here has been somewhat
helpful, but it appears I am missing the boat somewhere.

I have a web service I designed in VS 2005 that right now is doing
nothing more than returning an XML stream to a client. Eventually, it
will post an HTTP request to an outside server, but I'm trying to
learn VS 2005 as I go so I'm right now trying just to get a web
service to post to and it posts back what you post to it through
SOAP. This is the code:
8< snip

And this is the VB code that forms the client:

Imports System.IO
Imports System.Xml

Module Module1

Sub Main()
Dim msg As String
Dim strm As Stream
Dim strmrdr As System.IO.StreamReader
Dim http As New MSXML2.XMLHTTP30

Use a class in the .NET framework instead of that COM objects. Like
System.Net.HttpWebRequest or System.Net.WebClient.
Dim serviceurl As String = "http://localhost/Service1.asmx"
msg = "<soap:Envelope "
msg = msg & "xmlns:xsi='http://www.w3.org/2001/XMLSchema-
instance'"
msg = msg & "xmlns:xsi='http://www.w3.org/2001/XMLSchema'"
msg = msg & "xmlns:soap='http://schemas.xmlsoap.org/soap/
envelope'>"
msg = msg & "<soap:Body>"
msg = msg & "<AuthorizeAPI xmlns='http://badger.com'>"
msg = msg & "<Input1>"
msg = msg & "<SJName>Me</SJName>"
msg = msg & "<Email>[email protected]</Email>"
msg = msg & "</Input1>"
msg = msg & "</AuthorizeAPI>"
msg = msg & "</Soap:Body>"
msg = msg & "</Soap:Envelope>"

http.open("POST", serviceurl, False)
http.setRequestHeader("SOAPACTION", """http://badger.com/
AuthorizeAPI""")
http.setRequestHeader("Content-Type", "text/xml")
http.send(msg)
Console.Write(http.responseStream.ToString)
strm = http.responseStream

The ResponseStream of the COM object is not a System.IO.Stream object,
it's another COM object.
 
P

Patrice

You are mixing .NET objects and old plain COM objects. The stream returned
by the COM object is not a .NET stream !

Try :
http://support.microsoft.com/kb/308359/en-us (applies as well to VB.NET).

Basically you have just to put attributes for your web methods. The WSDL
file is create for you. When you add a web references to this WSDL file in
your client, a proxy is created for you and you just have to call the
methods. All the plumbing is done for you. Also the service should provide a
test page you can use to test this (check the .NET documentation for more
details).

If you still want to read the response yourself you could use
System.Net.WebClient to issue the HTTP request rather than MSXML2.
 
G

Guest

Patrice:

Using the web reference is fine, but, unfortunately, what I will eventually
be dealing with is a Java program that sends out an XML stream as a request
and I need to turn it over into an HTTP request for our provider, so I need
to make sure my service code can handle that.

The response should also be in XML format going back to the Java app.

Brent
 
J

John Saunders [MVP]

Brent White said:
Patrice:

Using the web reference is fine, but, unfortunately, what I will
eventually
be dealing with is a Java program that sends out an XML stream as a
request
and I need to turn it over into an HTTP request for our provider, so I
need
to make sure my service code can handle that.

The response should also be in XML format going back to the Java app.

What format is this XML stream from the Java program? Is it SOAP? Then they
can consume the web service with the Java equivalent of a web reference.

If it is not SOAP, then you're not going to be able to talk to the Java
program with a SOAP Web Service!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top