mccreaso

mccreaso

  • NA
  • 1
  • 3.3k

VB.net ASP.Net Web Service to Call a Web Service

Jan 30 2013 3:27 PM

Hello,

I hopeful that someone can provide me with a bit of help. I have developed the below ASP.net web service (web service A) that calls another web service B. The aim of the web service A is to accept bank account details as parameters and generate some SOAP XML and post this to web service B capturing the SOAP Envelope response from web service B and then passing this back to web service A. The issue is that I need pass back the SOAP envelope resonse from web service B back exactly to web service A and cannot get it right. I can get the SOAP response from web service B ok but there is additional XML at the end that relates to web service A. Basically my SOAP response in web service A is a combination of the response from B + the reponse from A. Hope this makes sense - maybe the code below will be easier to read!


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

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Name:="Bank Account Validation Web Service")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1
    Inherits System.Web.Services.WebService

    <WebMethod(Description:="This method accepts Bank Account details and sends to webservice")>
    <ServiceContract(Namespace:="
http://microsoft.wcf.documentation", Name:="SampleService", ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _
    Public Sub bankData(ByVal accountNumber As String, ByVal sortCode As String, ByVal country As String)

        'Retreive web service endpoint from web.config
        Dim endPoint As String = System.Web.Configuration.WebConfigurationManager.AppSettings("endPoint").ToString()

        'Define the SOAP XML and convert to Byte
        Dim bytArguments As Byte() = System.Text.Encoding.ASCII.GetBytes( _
        "<soapenv:Envelope xmlns:soapenv=""
http://schemas.xmlsoap.org/soap/envelope/"" xmlns:cm=""http://oracle.com/CM-BkWSS.xsd"">" & System.Environment.NewLine & _
        "   <soapenv:Header/>" & System.Environment.NewLine & _
        "   <soapenv:Body>" & System.Environment.NewLine & _
        "       <cm:CM-BkWSS faultStyle=""wsdl"">" & System.Environment.NewLine & _
        "           <cm:sortCode>" & sortCode & "</cm:sortCode>" & System.Environment.NewLine & _
        "           <cm:accountNumber>" & accountNumber & "</cm:accountNumber>" & System.Environment.NewLine & _
        "           <cm:country>" & country & "</cm:country>" & System.Environment.NewLine & _
        "       </cm:CM-BkWSS>" & System.Environment.NewLine & _
        "   </soapenv:Body>" & System.Environment.NewLine & _
        "</soapenv:Envelope>")

        ' Run the web service
        runWebService(bytArguments, endPoint)

    End Sub

    Sub runWebService(bytArgumentsVal As Byte(), endPoint As String)
        'Define web client var
        Dim manualWebClient As New System.Net.WebClient()

        'Get CC&B user credentials from web.config file
        Dim user As String = System.Web.Configuration.WebConfigurationManager.AppSettings("userVal").ToString()
        Dim password As String = System.Web.Configuration.WebConfigurationManager.AppSettings("passVal").ToString()

        ' Define content type and add CC&B login details to the HTTP header
        manualWebClient.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8")
        manualWebClient.Credentials = New NetworkCredential(user, password)

        Try
            'Post to web service
            Dim bytRetData As Byte() = manualWebClient.UploadData(endPoint, "POST", bytArgumentsVal)

            'Read response
            'Return System.Text.Encoding.ASCII.GetString(bytRetData)

            Context.Response.Write(System.Text.Encoding.ASCII.GetString(bytRetData))
        Catch x As System.Net.WebException
            ' Return errors
            'Return x.Message
            Context.Response.Write(x.Message)
        End Try
    End Sub
End Class

The above program will receive the following Request:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:bankData>
         <!--Optional:-->
         <tem:accountNumber>?</tem:accountNumber>
         <!--Optional:-->
         <tem:sortCode>?</tem:sortCode>
         <!--Optional:-->
         <tem:country>?</tem:country>
      </tem:bankData>
   </soapenv:Body>
</soapenv:Envelope>

And will return below - I do not want it to return the highlighted text:

<?xml version="1.0" ?>
<soapenv:Envelope xmlns:soapenv="
http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body >
<CM-BkWSS xmlns="http://oracle.com/CM-BkWSS.xsd" faultStyle="wsdl">

  <sortCode>?</sortCode>
  <accountNumber>?</accountNumber>
  <country>?</country>
  <result>
    <status>I</status>
    <validity>Invalid</validity>
    <errorCode>E00001</errorCode>
    <errorText>SOME ERROR TEXT.</errorText>
  </result>
</CM-BkWSS>
</soapenv:Body >
</soapenv:Envelope>
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="
http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><bankDataResponse xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>

Thanks in Advance

Sean