Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

BeginGetRequestStream with long POST

11 views
Skip to first unread message

Tosco

unread,
Jan 22, 2008, 12:31:20 AM1/22/08
to
After more than enough struggling I was finally able to POST some data
as explained in the example of BeginGetRequestStream.
But after some succesful tests with short ByteArrays I got stuck again
because it doesn't work with long strings.
I didn't make any formal test, but it looks like with the ByteArray <
200 bytes it works, with ByteArray > 1300 it fails.
The "myWebRequest.GetResponse()" throws the exception "The remote
server returned an error: (500) Internal Server Error."

I tried to send the buffer in smaller chunks, a loop with many
"streamResponse.Write", but it didn't help.

It is the first time I use WebRequest, I don't know what else to try,
so I'm trying here.

Thanks,
Stefano

Tosco

unread,
Jan 22, 2008, 11:16:57 PM1/22/08
to
This is the code.
Can anyone tell me if this is correct or if there is something wrong?

Thanks,
Stefano

Private Class RequestState
'see help for "WebRequest.BeginGetRequestStream Method"
Public Request As WebRequest
Public Sub New()
Request = Nothing
End Sub
End Class
Private MyPostData As String
Private AllDone As New Threading.ManualResetEvent(False)
Private Function GetPageWithPost(ByVal Uri As String, ByVal PostData
As String) As String
'see help for "WebRequest.BeginGetRequestStream Method"
' Create a new request.
Dim WebReq As HttpWebRequest = WebRequest.Create(Uri)
WebReq.CookieContainer = CookieCont
' Create an instance of the RequestState and assign
' WebReq' to it's request field.
Dim ReqState As New RequestState()
ReqState.Request = WebReq
WebReq.ContentType = "application/x-www-form-urlencoded"

' Set the 'Method' property to 'POST' to post data to a Uri.
ReqState.Request.Method = "POST"
MyPostData = PostData
' Start the asynchronous 'BeginGetRequestStream' method call.
Dim AsyncRes As IAsyncResult =
CType(WebReq.BeginGetRequestStream(AddressOf ReadCallback, ReqState),
IAsyncResult)
' Pause the current thread until the async operation completes.
AllDone.WaitOne()
' Send the Post and get the response.
Dim WebResp As WebResponse = WebReq.GetResponse()
Dim StreamResp As Stream = WebResp.GetResponseStream()
Dim StreamRead As New StreamReader(StreamResp)
Dim ReadBuff(256) As Char
GetPageWithPost = ""
Dim Count As Integer = StreamRead.Read(ReadBuff, 0, 256)
Do While Count > 0
Dim OutputData As New String(ReadBuff, 0, Count)
GetPageWithPost += OutputData
Count = StreamRead.Read(ReadBuff, 0, 256)
Loop

' Close the Stream Object.
StreamResp.Close()
StreamRead.Close()
' Release the HttpWebResponse Resource.
WebResp.Close()
End Function
Private Sub ReadCallback(ByVal AsyncRes As IAsyncResult)
'see help for "WebRequest.BeginGetRequestStream Method"
Dim ReqState As RequestState = CType(AsyncRes.AsyncState,
RequestState)
Dim WebReq As WebRequest = ReqState.Request
' End the request.
Dim StreamResp As Stream = WebReq.EndGetRequestStream(AsyncRes)
' Create a string that is to be posted to the uri.
Dim Encoder As New ASCIIEncoding()
' Convert the string into a byte array.
Dim ByteArray As Byte() = Encoding.UTF8.GetBytes(MyPostData)
' Write the data to the stream.
StreamResp.Write(ByteArray, 0, MyPostData.Length)
StreamResp.Close()
' Allow the main thread to resume.
AllDone.Set()
End Sub

0 new messages