Question Why does my soap request return error 500?

groadsvb

Well-known member
Joined
Nov 13, 2006
Messages
75
Programming Experience
Beginner
below is the code that I am using for my learning of this subject and the button1_click works. The button2_click does not. the service is a publicly hosted service. Can someone tell me if they see something wrong with the button2_click request? Thanks.


Imports System.Web
Imports System.Text.Encoding
Imports System.IO
Imports System
Public Class Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim responseData As String = String.Empty

Dim hwrequest As System.Net.HttpWebRequest
hwrequest = System.Net.WebRequest.Create("http://www.webservicex.net/BibleWebservice.asmx/GetBookTitles")
hwrequest.Method = "POST"

Dim POSTdata As String = "<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" & System.Environment.NewLine & _
"<soap:Body>" & System.Environment.NewLine & _
"<GetBookTitles xmlns=""http://www.webserviceX.NET"">" & System.Environment.NewLine & _
"</soap:Body>" & System.Environment.NewLine & _
"</soap:Envelope>"

Dim encoding As New Text.ASCIIEncoding() 'Use UTF8Encoding for XML requests
Dim postByteArray() As Byte = encoding.GetBytes(POSTdata)

hwrequest.ContentLength = postByteArray.Length
Dim postStream As IO.Stream = hwrequest.GetRequestStream()
postStream.Write(postByteArray, 0, postByteArray.Length)
postStream.Close()

Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
Dim responseStream As IO.StreamReader = _
New IO.StreamReader(hwresponse.GetResponseStream())
responseData = responseStream.ReadToEnd()
End If
hwresponse.Close()
txtBooks.Text = responseData
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

Dim responseData As String = String.Empty

Dim hwrequest As System.Net.HttpWebRequest

hwrequest = System.Net.WebRequest.Create("http://www.webservicex.net/BibleWebservice.asmx/GetBibleWordsbyKeyWord")
hwrequest.Method = "POST"

Dim POSTdata As String = "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:SOAP-ENC=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" & System.Environment.NewLine & _
"<soap:Body>" & System.Environment.NewLine & _
"<m:GetBibleWordsbyKeyWord xmlns:m=""http://www.webserviceX.NET"">" & System.Environment.NewLine & _
"<m:BibleWords>James</m:BibleWords>" & System.Environment.NewLine & _
"</m:GetBibleWordsbyKeyWord>" & System.Environment.NewLine & _
"</soap:Body>" & System.Environment.NewLine & _
"</soap:Envelope>"

Dim encoding As New Text.ASCIIEncoding() 'Use UTF8Encoding for XML requests
Dim postByteArray() As Byte = encoding.GetBytes(POSTdata)

hwrequest.ContentLength = postByteArray.Length
Dim postStream As IO.Stream = hwrequest.GetRequestStream()
postStream.Write(postByteArray, 0, postByteArray.Length)
postStream.Close()

Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
Dim responseStream As IO.StreamReader = _
New IO.StreamReader(hwresponse.GetResponseStream())
responseData = responseStream.ReadToEnd()
End If
hwresponse.Close()
txtBooks.Text = responseData

End Sub
End Class
 
I recommend you add a Service Reference and consume the service through the generated proxy classes.
How to: Add a Reference to a Web Service
Here's an example of calling GetBookTitles method:
        Dim bib As New ServiceReference1.BibleWebserviceSoapClient("BibleWebserviceSoap")
        Dim titles = bib.GetBookTitles
 
During my recent education on web serivces I would agree but I am required to use httpWebRequest so I am trying to figure out how all the parts in the wsdl fit together so I an incorporate it into our application.
 
The service method page shows examples of request/response messages: BibleWebservice Web Service
It is also possible to use proxy to make requests, and to learn what's going on use a debugger like Fiddler to view the raw data that is sent and received.

edit: A word of warning if you're going to test that second method using the proxy, it can generate huge responses that both exceed the default receive message size and xml string deserialization length, here's an example where I up'ed the default binding limits enough to successfully complete the request:
        Dim bib As New ServiceReference3.BibleWebserviceSoapClient("BibleWebserviceSoap")
        With CType(bib.Endpoint.Binding, ServiceModel.BasicHttpBinding)
            .MaxBufferSize = 65000000
            .MaxReceivedMessageSize = .MaxBufferSize
            .ReaderQuotas.MaxStringContentLength = 10000000
        End With
        Dim s = bib.GetBibleWordsbyKeyWord("James")

The soap message that was sent according to Fiddler was this, not too different from the example request:
HTML:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <GetBibleWordsbyKeyWord xmlns="http://www.webserviceX.NET">
      <BibleWords>James</BibleWords>
    </GetBibleWordsbyKeyWord>
  </s:Body>
</s:Envelope>
 
Wow I did just those things JohnH. Since I am just learning this stuff it is good to know that I am doing something right. I was able figure it out by creating a WCF client application and using Fiddler2 to see what was transmitted. It came down to a couple of things. Here is what worked:

Dim responseData As String = String.Empty

Dim hwrequest As System.Net.HttpWebRequest

hwrequest = System.Net.HttpWebRequest.Create("http://www.webservicex.net/BibleWebservice.asmx")
hwrequest.Method = "POST"

hwrequest.ContentType = "text/xml; charset=utf-8"


Dim POSTdata As String = "<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/""><s:Body><GetBibleWordsByBookTitleAndChapter xmlns=""http://www.webserviceX.NET"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><BookTitle>Mark</BookTitle><chapter>1</chapter></GetBibleWordsByBookTitleAndChapter></s:Body></s:Envelope>"

Dim encoding As New Text.ASCIIEncoding() 'Use UTF8Encoding for XML requests
Dim postByteArray() As Byte = encoding.GetBytes(POSTdata)

hwrequest.ContentLength = postByteArray.Length
Dim postStream As IO.Stream = hwrequest.GetRequestStream()
postStream.Write(postByteArray, 0, postByteArray.Length)
postStream.Close()
Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
Try

If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
Dim responseStream As IO.StreamReader = _
New IO.StreamReader(hwresponse.GetResponseStream())
responseData = responseStream.ReadToEnd()
End If

Catch ex As Exception
responseData = ex.Message.ToString
End Try
hwresponse.Close()
txtBox2.Text = responseData
 
I was editing my reply while running some tests, so it took a while. Previous reply was updated with some new info and code.
 
Back
Top