Solved

How to pass soap xml request in this code

Posted on 2011-03-24
7
1,032 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
[X]
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
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

622 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