Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to pass soap xml request in this code

Posted on 2011-03-24
7
1,018 Views
Last Modified: 2012-05-11
My Code
-----------
Private Sub GetResponseAsString(ByVal postData As String, ByVal timeout As Integer)    
        ' Create a request using a URL that can receive a post.
        Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(postData), HttpWebRequest)
        ' Set the Method property of the request to POST.
        webRequest.Headers.Clear()
        webRequest.AllowAutoRedirect = True
        webRequest.Timeout = 1000 * timeout
        webRequest.PreAuthenticate = True
        webRequest.ContentType = "application / x - www - form - urlencoded"
        webRequest.Credentials = CredentialCache.DefaultCredentials
        webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"
        webRequest.Timeout = 150000
        webRequest.Method = "POST"

        Dim webResponse As WebResponse = Nothing      
        Dim objStream As Stream        
        Dim objSR As StreamReader
        Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")      
        Dim sResponse As String
        webResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
        objStream = webResponse.GetResponseStream()      
        objSR = New StreamReader(objStream, encode, True)
        ''<<sResponse doesn't contain Unicode char values>>
        sResponse = objSR.ReadToEnd()        
        Response.Write(sResponse)      
    End Sub

My Soap XML Request
---------------------------
<?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" xmlns:tns="http://stamps.com/xml/namespace/2009/08/swsim/swsimv6">
  <soap:Body>
    <tns:AuthenticateUser>
      <tns:Credentials>
        <tns:IntegrationID>abc</tns:IntegrationID>
        <tns:Username>test1</tns:Username>
        <tns:Password>test1</tns:Password>
      </tns:Credentials>
    </tns:AuthenticateUser>
  </soap:Body>
</soap:Envelope>

- Please help me out.
- How to send my SOAP XML request to above my code. so i can get proper respose.
- Please applied in my code and reply me as soon as possible.

0
Comment
Question by:citadelind
  • 4
  • 3
7 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35207050
Try adding the following after webRequest.Method = "POST"
Dim encoding As Encoding = Encoding.GetEncoding("UTF-8")
Dim buffer As Byte() = encoding.GetBytes("Your SOAP Request")
webRequest.ContentLength = buffer.Length

Using stream As System.IO.Stream = webRequest.GetRequestStream()
     stream.Write(buffer, 0, buffer.Length)
End Using

Open in new window

0
 

Author Comment

by:citadelind
ID: 35207396
I applied your code as below
---------------------------------------

 Private Sub GetResponseAsString(ByVal postData As String, ByVal timeout As Integer)
        Dim xmlfile As String
        xmlfile = Server.MapPath("XMLFile.xml")
        Dim doc As New XmlDocument()
        doc.Load(Convert.ToString(xmlfile))
        Dim data As String = xmlfile
        ' Create a request using a URL that can receive a post.
        Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(postData), HttpWebRequest)
        ' Set the Method property of the request to POST.
        webRequest.Headers.Clear()
        webRequest.AllowAutoRedirect = True
        webRequest.Timeout = 1000 * timeout
        webRequest.PreAuthenticate = True
        webRequest.ContentType = "application / x - www - form - urlencoded"
        webRequest.Credentials = CredentialCache.DefaultCredentials
        webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"
        webRequest.Timeout = 150000      

        Dim webResponse As WebResponse = Nothing      
        Dim objStream As Stream
        Dim objSR As StreamReader

        Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")
        Dim str As String
        str = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:sws='http://stamps.com/xml/namespace/2010/11/swsim/swsimv12'>"
        str = str & "<soapenv:Header/>"
        str = str & "<soapenv:Body>"
        str = str & "<sws:AuthenticateUser>"
        str = str & "<sws:Credentials>"
        str = str & "<sws:IntegrationID>test</sws:IntegrationID>"
        str = str & "<sws:Username>test</sws:Username>"
        str = str & "<sws:Password>test</sws:Password>"
        str = str & "</sws:Credentials>"
        str = str & "</sws:AuthenticateUser>"
        str = str & "</soapenv:Body>"
        str = str & "</soapenv:Envelope>"
        Dim buffer As Byte() = encode.GetBytes(str)
        webRequest.ContentLength = buffer.Length
        Using stream As System.IO.Stream = webRequest.GetRequestStream()
            stream.Write(buffer, 0, buffer.Length)
        End Using
        webRequest.Method = "POST"
        Dim sResponse As String
        webResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
        objStream = webResponse.GetResponseStream()      
        objSR = New StreamReader(objStream, encode, True)
        ''<<sResponse doesn't contain Unicode char values>>
        sResponse = objSR.ReadToEnd()        
        Response.Write(sResponse)
End sub

--------------------------------------------
I got error
---------------------------------------------

Cannot send a content-body with this verb-type.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.

Source Error:

Line 182:        Dim buffer As Byte() = encode.GetBytes(str)
Line 183:        webRequest.ContentLength = buffer.Length
Line 184:        Using stream As System.IO.Stream = webRequest.GetRequestStream()
Line 185:            stream.Write(buffer, 0, buffer.Length)
Line 186:        End Using


- Please help me out where the problem
0
 

Author Comment

by:citadelind
ID: 35207446
Please ignore previous post I applied your code as below
---------------------------------------------------------------------------

Private Sub GetResponseAsString(ByVal postData As String, ByVal timeout As Integer)
        Dim xmlfile As String
        xmlfile = Server.MapPath("XMLFile.xml")
        Dim doc As New XmlDocument()
        doc.Load(Convert.ToString(xmlfile))
        Dim data As String = xmlfile
        ' Create a request using a URL that can receive a post.
        Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(postData), HttpWebRequest)
        ' Set the Method property of the request to POST.
        webRequest.Headers.Clear()
        webRequest.AllowAutoRedirect = True
        webRequest.Timeout = 1000 * timeout
        webRequest.PreAuthenticate = True
        webRequest.ContentType = "application / x - www - form - urlencoded"
        webRequest.Credentials = CredentialCache.DefaultCredentials
        webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"
        webRequest.Timeout = 150000      
        webRequest.Method = "POST"
        Dim webResponse As WebResponse = Nothing      
        Dim objStream As Stream
        Dim objSR As StreamReader

        Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")
        Dim str As String
        str = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:sws='http://stamps.com/xml/namespace/2010/11/swsim/swsimv12'>"
        str = str & "<soapenv:Header/>"
        str = str & "<soapenv:Body>"
        str = str & "<sws:AuthenticateUser>"
        str = str & "<sws:Credentials>"
        str = str & "<sws:IntegrationID>E5248DFD-E208-49B9-8151-14BB17FB1CE2</sws:IntegrationID>"
        str = str & "<sws:Username>test</sws:Username>"
        str = str & "<sws:Password>test</sws:Password>"
        str = str & "</sws:Credentials>"
        str = str & "</sws:AuthenticateUser>"
        str = str & "</soapenv:Body>"
        str = str & "</soapenv:Envelope>"
        Dim buffer As Byte() = encode.GetBytes(str)
        webRequest.ContentLength = buffer.Length
        Using stream As System.IO.Stream = webRequest.GetRequestStream()
            stream.Write(buffer, 0, buffer.Length)
        End Using      
        Dim sResponse As String
        webResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
        objStream = webResponse.GetResponseStream()      
        objSR = New StreamReader(objStream, encode, True)
        ''<<sResponse doesn't contain Unicode char values>>
        sResponse = objSR.ReadToEnd()        
        Response.Write(sResponse)
        ' OR Response.write(HttpUtility.HtmlEncode(sResponse))

    End Sub

--------------------------------------------
I got error
---------------------------------------------

The remote server returned an error: (500) Internal Server Error.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned an error: (500) Internal Server Error.

Source Error:

Line 186:        End Using      
Line 187:        Dim sResponse As String
Line 188:        webResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
Line 189:        objStream = webResponse.GetResponseStream()      
Line 190:        objSR = New StreamReader(objStream, encode, True)


- Please help me out where the problem
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35207470
Notice in my first post i said AFTER the line webRequest.Method = "POST"
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35207486
That could be any number of things. What exactly is it you are trying to send data to?
0
 

Author Comment

by:citadelind
ID: 35207527
see below my code
-----------------------------

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
        GetResponseAsString("https://swsim.testing.stamps.com/swsim/SwsimV12.asmx", "10000")
End Sub


Private Sub GetResponseAsString(ByVal postData As String, ByVal timeout As Integer)
        Dim xmlfile As String
        xmlfile = Server.MapPath("XMLFile.xml")
        Dim doc As New XmlDocument()
        doc.Load(Convert.ToString(xmlfile))
        Dim data As String = xmlfile
        ' Create a request using a URL that can receive a post.
        Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(postData), HttpWebRequest)
        ' Set the Method property of the request to POST.
        webRequest.Headers.Clear()
        webRequest.AllowAutoRedirect = True
        webRequest.Timeout = 1000 * timeout
        webRequest.PreAuthenticate = True
        webRequest.ContentType = "application / x - www - form - urlencoded"
        webRequest.Credentials = CredentialCache.DefaultCredentials
        webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"
        webRequest.Timeout = 150000      
        webRequest.Method = "POST"
        Dim webResponse As WebResponse = Nothing      
        Dim objStream As Stream
        Dim objSR As StreamReader

        Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")
        Dim str As String
        str = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:sws='http://stamps.com/xml/namespace/2010/11/swsim/swsimv12'>"
        str = str & "<soapenv:Header/>"
        str = str & "<soapenv:Body>"
        str = str & "<sws:AuthenticateUser>"
        str = str & "<sws:Credentials>"
        str = str & "<sws:IntegrationID>E5248DFD-E208-49B9-8151-14BB17FB1CE2</sws:IntegrationID>"
        str = str & "<sws:Username>test</sws:Username>"
        str = str & "<sws:Password>test</sws:Password>"
        str = str & "</sws:Credentials>"
        str = str & "</sws:AuthenticateUser>"
        str = str & "</soapenv:Body>"
        str = str & "</soapenv:Envelope>"
        Dim buffer As Byte() = encode.GetBytes(str)
        webRequest.ContentLength = buffer.Length
        Using stream As System.IO.Stream = webRequest.GetRequestStream()
            stream.Write(buffer, 0, buffer.Length)
        End Using      
        Dim sResponse As String
        webResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
        objStream = webResponse.GetResponseStream()      
        objSR = New StreamReader(objStream, encode, True)
        ''<<sResponse doesn't contain Unicode char values>>
        sResponse = objSR.ReadToEnd()        
        Response.Write(sResponse)
        ' OR Response.write(HttpUtility.HtmlEncode(sResponse))

    End Sub

--------------------------------------------
I got error
---------------------------------------------

The remote server returned an error: (500) Internal Server Error.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned an error: (500) Internal Server Error.

Source Error:

Line 186:        End Using      
Line 187:        Dim sResponse As String
Line 188:        webResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
Line 189:        objStream = webResponse.GetResponseStream()      
Line 190:        objSR = New StreamReader(objStream, encode, True)

- Check bold value. where i send.
- check it out.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 35207635
I get a 405 (Method not allowed) when I try it. Which suggests that your SOAP payload is invalid, or there is an authentication issue. The URL you are using won't resolve in a browser so I have no way of testing it.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
The viewer will learn how to dynamically set the form action using jQuery.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question