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
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