I'm trying to use the web request class in vb.net to post some xml. On the reciving end, I keep getting an internal server error with the message: Missing Soap action header". I did some reasearch and saw that a firewall can strip out the soap action header so I tried running it with no firewall and got the same result. I tried adding a soap action header using the header command, and that didn't even get to the server to receive an error message. Can anyone tell me how to add the soap action header? Code is below:

Public Sub PostToWebsite(ByVal sData As String)
Dim oRequest As WebRequest = WebRequest.Create(cDestinationURL)
oRequest.Method = "POST"

'convert data to a byte array
Dim bArray As Byte() = Encoding.UTF8.GetBytes(sData)

'set content type
oRequest.ContentType = "application/x-www-form-urlencoded"

' soap action (this is the header I tried to add. Didn't work.)
' oRequest.Headers.Add("SOAPAction", "\ \ ")

' Set the ContentLength property of the WebRequest.
oRequest.ContentLength = bArray.Length

' Get the request stream.
Dim dataStream As Stream = oRequest.GetRequestStream

' Write the data to the request stream.
dataStream.Write(bArray, 0, bArray.Length)

Catch ex As Exception
cPostResponse = "Error Sending Data: " & ex.Message
Exit Sub
End Try

' Close the Stream object.

' Get the response.
Dim oResponse As WebResponse = oRequest.GetResponse()
dataStream = oResponse.GetResponseStream()

Catch ex As Exception
cPostResponse = "Error Getting Response: " & ex.Message
Exit Sub
End Try

' Get the stream containing content returned by the server.

' Open the stream using a StreamReader for easy access.
Dim oReader As New StreamReader(dataStream)

' Read the content.
cPostResponse = oReader.ReadToEnd()

' Clean up the streams.

End Sub

The second parameter needs to be a URI. I changed three lines in your code and it works just fine.

Dim oRequest As WebRequest = webRequest.Create("http://www.webservicex.net/stockquote.asmx")

        oRequest.ContentType = "text/xml" 
        oRequest.Headers.Add("SOAPAction", "http://www.webserviceX.NET/GetQuote")

and sData =

<?xml version="1.0" encoding="utf-8"?>
	<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
		<GetQuote xmlns="http://www.webserviceX.NET/">

Hope that helps

This article has been dead for over six months. Start a new discussion instead.