473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

500 status in webrequest

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

Mar 24 '06 #1
0 1187

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: James Johnson | last post by:
Dear C#dex, I define a variable: HttpWebRequest webRequest and run the following request webRequest = WebRequest.Create(TARGET_URL) as HttpWebRequest; The webRequest object returns values...
3
by: Paul | last post by:
Hello, First I want to refer to the problem "WebRequest : execute a button" of a few days ago. The way I solved it, I loose my session, and as a consequence my session variables. I don't want...
8
by: John K. | last post by:
Hi I was wondering if it's possible to use the WebRequest class to access a file on windows shared folder with authentication? If yes, what would the syntax be? I've tried to look this up in the...
12
by: ThyRock | last post by:
I am working on a WebRequest accessing the US Postal Service WebTools test API. This service uses a DLL file (ShippingAPITest.dll) with a query string which includes XML. The web service accepts...
2
by: kkb | last post by:
Hello! First, I'm sorry because of my english... I'll try to be understandable! I've got a strange problem using .NET 2003 C# and I haven't figured it out for a long time. I'm implementing an...
1
by: Mr Flibble | last post by:
OK I logon to a web site and I manage to get an SMSESSION cookie that I then store in a variable called _session (a class scoping variable). I do this by calling a logon URL and setting a cookie...
3
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I'm trying to create a call to a web page to validate and register software. The code I'm using is: Private Sub OK_Click(ByVal sender As...
3
by: Dave | last post by:
string m_request = some_web_page; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_request ); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Which works...
7
by: darrel | last post by:
I swear I've read you can do this somewhere before, but don't recall where or what it'd even be called to do the correct googling. What I've been asked to do is make a 'dashboard' app that checks...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.