Hi,
I do an asp.net page where I will display products through JSON.
I searched on Web and i found these implementation:
Code:
Public Class RequestState
Public Property BufferSize As Integer
Public Property ResponseContent As StringBuilder
Public Property BufferRead As Byte()
Public Property Request As HttpWebRequest
Public Property Response As HttpWebResponse
Public Property ResponseStream As Stream
Public Sub New()
BufferSize = 1024
BufferRead = New Byte(BufferSize - 1) {}
ResponseContent = New StringBuilder()
Request = Nothing
ResponseStream = Nothing
End Sub
End Class
Private Sub ResponseCallback(ByVal result As IAsyncResult)
Try
Dim state As RequestState = CType(result.AsyncState, RequestState)
Dim request As HttpWebRequest = state.Request
state.Response = CType(request.EndGetResponse(result), HttpWebResponse)
Dim responseStream As Stream = state.Response.GetResponseStream()
state.ResponseStream = responseStream
Dim readResult As IAsyncResult = responseStream.BeginRead(state.BufferRead, 0, state.BufferSize, New AsyncCallback(AddressOf ReadCallback), state)
Catch ex As Exception
Dim state As RequestState = CType(result.AsyncState, RequestState)
If state.Response IsNot Nothing Then state.Response.Close()
End Try
End Sub
Private Sub ReadCallback(ByVal result As IAsyncResult)
Try
Dim state As RequestState = CType(result.AsyncState, RequestState)
Dim bytesRead As Integer = state.ResponseStream.EndRead(result)
If bytesRead > 0 Then
state.ResponseContent.Append(Encoding.ASCII.GetString(state.BufferRead, 0, bytesRead))
state.ResponseStream.BeginRead(state.BufferRead, 0, state.BufferSize, New AsyncCallback(AddressOf ReadCallback), state)
Else
If state.ResponseContent.Length > 0 Then
Dim AsyncResponseContent = state.ResponseContent.ToString()
state.ResponseStream.Close()
state.Response.Close()
'OnAsyncResponseArrived(AsyncResponseContent)
End If
End If
Catch ex As Exception
Dim state As RequestState = CType(result.AsyncState, RequestState)
If state.Response IsNot Nothing Then state.Response.Close()
End Try
End Sub
Public Sub MakeWebRequestAsync(ByVal url As String)
Try
Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
request.Method = "GET"
request.Proxy = Nothing
Dim state As RequestState = New RequestState()
state.Request = request
Dim result As IAsyncResult = request.BeginGetResponse(New AsyncCallback(AddressOf ResponseCallback), state)
Catch ex As Exception
End Try
End Sub
How can i view it when I clicked a button?