I'm attempting to update a legacy VB6 component (not written by me) to the .NET platform. There is one function which posts an XML string to a URL:
Function PostToUrl(ByRef psUrl, ByRef psData, Byref psResponseText, ByRef psErrorMsg, ByRef psUsername, ByRef psPassword)
On Error Resume Next
Dim objWinHTTP
PostToUrl = False
psErrorMsg = ""
psResponseText = ""
Dim m_lHTTPREQUEST_SETCREDENTIALS_FOR_SERVER
m_lHTTPREQUEST_SETCREDENTIALS_FOR_SERVER =0
Set objWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objWinHTTP.Open "POST", psUrl
If psUsername <> "" Then
Call objWinHTTP.SetCredentials(psUsername, psPassword, m_lHTTPREQUEST_SETCREDENTIALS_FOR_SERVER)
End If
objWinHTTP.SetRequestHeader "Host", "http://www.xxx.com/CSIHTTP.asp"
objWinHTTP.SetRequestHeader "X-Method", "Submit"
objWinHTTP.SetRequestHeader "Content-Type", "text/xml"
objwinHTTP.setTimeouts 3000, 15000, 15000, 120000
Call objWinHTTP.Send(psData)
' Handle errors '
If Err.Number <> 0 Then
psErrorMsg = Err.Description & " test"
PostToUrl = False
ElseIf objWinHTTP.Status <> 200 AND objWinHTTP.Status <> 202 Then
psErrorMsg = "(" & objWinHTTP.Status & ") " & objWinHTTP.StatusText
PostToUrl = False
Else
psErrorMsg = objWinHTTP.ResponseText & "(" & objWinHTTP.Status & ") " & objWinHTTP.StatusText
PostToUrl = True
End If
Set objWinHTTP = Nothing
End Function
I've updated this to:
Public Function PostXml(ByVal XML As String) As Boolean
Try
Dim URL As String = My.Settings.NTSPostURL
'TODO: supply username and password! '
Dim Bytes As Byte() = Me.Encoding.GetBytes(XML)
Dim HTTPRequest As HttpWebRequest = DirectCast(WebRequest.Create(Me.PostURL), HttpWebRequest)
Dim Cred As New NetworkCredential("username", "password", "http://www.xxx.com")
With HTTPRequest
.Method = "POST"
.ContentLength = Bytes.Length
.ContentType = "text/xml"
.Credentials = Cred
.Timeout = 120000
.Method = "Submit"
End With
Using RequestStream As Stream = HTTPRequest.GetRequestStream()
RequestStream.Write(Bytes, 0, Bytes.Length)
RequestStream.Close()
End Using
Using Response As HttpWebResponse = DirectCast(HTTPRequest.GetResponse(), HttpWebResponse)
If Response.StatusCode <> HttpStatusCode.OK Then
Dim message As String = [String].Format("POST failed. Received HTTP {0}", Response.StatusCode)
Throw New ApplicationException(message)
End If
End Using
Catch ex As WebException
Dim s As String = ex.Response.ToString() & " " & ex.Status.ToString()
Throw
End Try
End Function
However when I run the .NET code the server returns the error '403 Forbidden - protocol error' on the line:
Using Response As HttpWebResponse = DirectCast(HTTPRequest.GetResponse(), HttpWebResponse)
. The VB6 code runs fine. Can anyone identify any discrepancies between the two that might be causing this? It's left me stumped....