How to pass soap xml request in this code

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.

citadelindAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
That could be any number of things. What exactly is it you are trying to send data to?
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
citadelindAuthor Commented:
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
citadelindAuthor Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
Notice in my first post i said AFTER the line webRequest.Method = "POST"
0
 
citadelindAuthor Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.