Reputation: 753
I am trying to perform a XML Soap call in a VB application to a web service. I can get this to work using SoapUI and XMLSpy, which leads me to believe that the XML is correct. When I perform the code I get an error 500 Server Internal Error. Which leads me to believe I am missing something in the code and not in the XML. I did try adding a Server Reference, but that didn't seem to help either.
I used this video as a reference: "Visual Basic - WebRequests VB.net Intermediate" My code looks like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim SoapByte() As Byte
Dim SoapStr As String = ""
SoapStr = SoapStr & "<?xml version=""1.0"" encoding=""UTF-8""?>"
SoapStr = SoapStr & "<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:web=""http://webservice.lme.com"">"
SoapStr = SoapStr & "<soapenv:Header/>"
SoapStr = SoapStr & "<soapenv:Body>"
SoapStr = SoapStr & "<web:getCustomerRate>"
SoapStr = SoapStr & "<web:criIn>"
SoapStr = SoapStr & "<web:protectFreeze>Y</web:protectFreeze>"
SoapStr = SoapStr & "<web:accountNumber>226744</web:accountNumber>"
SoapStr = SoapStr & "<web:callConsgBfrDel>No</web:callConsgBfrDel>"
SoapStr = SoapStr & "<web:callforAppmnt>No</web:callforAppmnt>"
SoapStr = SoapStr & "<web:callforCarrierConv>No</web:callforCarrierConv>"
SoapStr = SoapStr & "<web:COD>N</web:COD>"
SoapStr = SoapStr & "<web:CODAmount>0</web:CODAmount>"
SoapStr = SoapStr & "<web:CODPayTerms>0</web:CODPayTerms>"
SoapStr = SoapStr & "<!--1 or more repetitions:-->"
SoapStr = SoapStr & "<web:commLines>"
SoapStr = SoapStr & "<web:commClass>100</web:commClass>"
SoapStr = SoapStr & "<web:commweight>1000</web:commweight>"
SoapStr = SoapStr & "</web:commLines>"
SoapStr = SoapStr & "<web:customerType>S</web:customerType>"
SoapStr = SoapStr & "<web:destCity>Lake City</web:destCity>"
SoapStr = SoapStr & "<web:destState>MN</web:destState>"
SoapStr = SoapStr & "<web:destZip>55041</web:destZip>"
SoapStr = SoapStr & "<web:fullValueCoverage>No</web:fullValueCoverage>"
SoapStr = SoapStr & "<web:fvcAmount>0</web:fvcAmount>"
SoapStr = SoapStr & "<web:hazMat>N</web:hazMat>"
SoapStr = SoapStr & "<web:insideDel>No</web:insideDel>"
SoapStr = SoapStr & "<web:insidePckup>No</web:insidePckup>"
SoapStr = SoapStr & "<web:lftgtReqDel>No</web:lftgtReqDel>"
SoapStr = SoapStr & "<web:lftgtRqrdPckup>No</web:lftgtRqrdPckup>"
SoapStr = SoapStr & "<web:lmtAccessDel>No</web:lmtAccessDel>"
SoapStr = SoapStr & "<web:lmtdAccessPckup>No</web:lmtdAccessPckup>"
SoapStr = SoapStr & "<web:origCity>Burnsville</web:origCity>"
SoapStr = SoapStr & "<web:origState>MN</web:origState>"
SoapStr = SoapStr & "<web:origZip>55337</web:origZip>"
SoapStr = SoapStr & "<web:palletCount>1</web:palletCount>"
SoapStr = SoapStr & "<web:palletType>PALLETS</web:palletType>"
SoapStr = SoapStr & "<web:password></web:password>"
SoapStr = SoapStr & "<web:paymentType>P</web:paymentType>"
SoapStr = SoapStr & "<web:prvtRsdncDel>No</web:prvtRsdncDel>"
SoapStr = SoapStr & "<web:prvtRsdncPckup>No</web:prvtRsdncPckup>"
SoapStr = SoapStr & "<web:shipDate>10/02/2017</web:shipDate>"
SoapStr = SoapStr & "<web:usePalletPricing>No</web:usePalletPricing>"
SoapStr = SoapStr & "<web:username></web:username>"
SoapStr = SoapStr & "</web:criIn>"
SoapStr = SoapStr & "</web:getCustomerRate>"
SoapStr = SoapStr & "</soapenv:Body>"
SoapStr = SoapStr & "</soapenv:Envelope>"
goUrl.Text = "Http://www.lme4me.com:80/webapp/CustomPricing/services/CRIMethods"
Try
SoapByte = Encoding.UTF8.GetBytes(SoapStr)
'Create initial request
Dim request As HttpWebRequest = HttpWebRequest.Create(goUrl.Text)
request.Proxy = Nothing 'Speeds up the request by setting the proxies to nothing
request.UserAgent = "Test" 'We should not be worried about this.
request.PreAuthenticate = False
request.Method = "POST"
request.ContentType = "Text/ Xml;charset=UTF-8"
'request.Headers.Add("SOAPAction", "http://www.lme4me.com:80/webapp/CustomPricing/services/CRIMethods/getCustomerRate")
request.Headers.Add("SOAPAction", "getCustomerRate")
'request.ContentLength = 2215
request.Host = "www.lme4me.com:80"
'request.Connection = "Keep-Alive"
request.UserAgent = "Apache-HttpClient / 4.1.1 (java 1.5)"
'Create the Response and reader
Dim response As HttpWebResponse = request.GetResponse()
Dim responseHdr As String = request.GetResponse.ResponseUri.ToString()
Dim responseStream As IO.Stream = response.GetResponseStream()
'Create a new Stream reader
Dim streamReader As New System.IO.StreamReader(responseStream)
Dim Data As String = streamReader.ReadToEnd
streamReader.Close()
' Display the Data on the screen
UrlResponseText.Text = Data
Catch ex As WebException
MsgBox("Inproper input: " + Err.Description)
goUrl.Text = ""
If (ex.Status = WebExceptionStatus.ProtocolError) Then
Dim error1 As String = New IO.StreamReader(ex.Response.GetResponseStream()).ReadToEnd()
MsgBox("Rest of Error: " + error1.ToString())
End If
End Try
End Sub
Private Sub goUrl_TextChanged(sender As Object, e As EventArgs) Handles goUrl.TextChanged
End Sub
End Class
I can not seem to find much help for doing this in Visual Basic / VB.
Upvotes: 0
Views: 13908
Reputation: 753
I had found that I was not passing the SoapStr and needed to add some additional code to write it.
DataStream = Request.GetRequestStream() DataStream.Write(SoapByte, 0, SoapByte.Length) DataStream.Close()
So now the code looks like this:
SoapByte = System.Text.Encoding.UTF8.GetBytes(SoapStr)
'Create initial request
Request = WebRequest.Create(goUrl.Text)
Request.Headers.Add("SOAPAction", "http://www.lme4me.com:80/webapp/CustomPricing/services/CRIMethods/getCustomerRate")
Request.ContentType = "Text/ Xml; charset=UTF-8"
Request.ContentLength = SoapByte.Length
Request.Method = "POST"
DataStream = Request.GetRequestStream()
DataStream.Write(SoapByte, 0, SoapByte.Length)
DataStream.Close()
Response = Request.GetResponse()
DataStream = Response.GetResponseStream()
Reader = New StreamReader(DataStream)
Dim Data As String = Reader.ReadToEnd()
DataStream.Close()
Reader.Close()
Response.Close()
' Display the Data on the screen
UrlResponseText.Text = Data
This seems to work correctly for me.
Upvotes: 0
Reputation: 960
I cannot see in your code where you are actually using your soapStr/soapByte.
Here is some generic code we are using:
Friend Function PostWebservice(soapAction As String, xmlBody As String) As XmlDocument
Dim uTF8Encoding As New UTF8Encoding()
Dim bytes As Byte() = uTF8Encoding.GetBytes(xmlBody)
Dim requestUriString As String = "Whatever.com"
Dim httpWebRequest As HttpWebRequest = CType(WebRequest.Create(requestUriString), HttpWebRequest)
httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate")
httpWebRequest.Headers.Add("SOAPAction", soapAction)
httpWebRequest.Method = "POST"
httpWebRequest.ContentType = "text/xml; charset=utf-8"
httpWebRequest.ContentLength = CLng(bytes.Length)
Dim requestStream As Stream = httpWebRequest.GetRequestStream()
requestStream.Write(bytes, 0, bytes.Length)
requestStream.Close()
Dim httpWebResponse As HttpWebResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)
Dim streamReader As StreamReader = New StreamReader(httpWebResponse.GetResponseStream(), uTF8Encoding)
Dim xmlDocument As New XmlDocument()
xmlDocument.LoadXml(streamReader.ReadToEnd())
httpWebResponse.Close()
Return xmlDocument
End Function
Upvotes: 1