Hi guys,
when i use winhttp, and the server returns status 500, I get the text
that the server returned anyway...
when i use webrequest, and the server returns status 500, webrequest
just throws an exception... How can i find the text that came back with
the 500 status?
I need this because i am posting to a web service, that returns soap
fault in xml format.
any help would be appreciated. below is my webrequest code:
Public Function URL_POSTDATA(ByVal URL As String, ByVal PostData As
String, Optional ByVal RequestHeaders As Hashtable = Nothing, Optional
ByVal UserName As String = "", Optional ByVal Password As String = "")
As String
Dim result As System.Net.WebResponse
Dim html As String = ""
Dim rex As Exception = Nothing
Try
Dim req As System.Net.WebRequest
Dim RequestStream As System.IO.Stream
Dim ReceiveStream As System.IO.Stream
Dim encode As System.Text.Encoding
Dim sr As System.IO.StreamReader
Dim nc As System.Net.NetworkCredential
req = System.Net.WebRequest.Create(URL)
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
If Not RequestHeaders Is Nothing Then
For Each Header As String In RequestHeaders.Keys
If Header.ToLower = "content-type" Then
req.ContentType = RequestHeaders(Header)
Else
req.Headers.Add(Header, RequestHeaders(Header))
End If
Next
RequestHeaders = Nothing
End If
If UserName <> "" Then
nc = New System.Net.NetworkCredential(UserName,
Password)
req.Credentials = nc
End If
Dim SomeBytes() As Byte
Dim UrlEncoded As New System.Text.StringBuilder
'Dim reserved() As Char = {ChrW(63), ChrW(61), ChrW(38)}
SomeBytes = System.Text.Encoding.UTF8.GetBytes(PostData)
req.ContentLength = SomeBytes.Length
RequestStream = req.GetRequestStream()
RequestStream.Write(SomeBytes, 0, SomeBytes.Length)
RequestStream.Close()
result = req.GetResponse()
ReceiveStream = result.GetResponseStream()
encode = System.Text.Encoding.GetEncoding("utf-8")
sr = New System.IO.StreamReader(ReceiveStream, encode)
Dim read(256) As Char
Dim count As Integer = sr.Read(read, 0, 256)
Do While count > 0
html &= New String(read, 0, count)
count = sr.Read(read, 0, 256)
Loop
Catch Exc As Exception
rex = Exc
Finally
If Not result Is Nothing Then
result.Close()
End If
End Try
If Not rex Is Nothing Then
Err.Raise(1, "URL_POST()", "Network Error: The request URI
could not be loaded or was malformed: " & rex.Message)
End If
Return html
End Function