Re: Asynchronous HttpWebRequest
From: bruce barker (nospam_brubar_at_safeco.com)
Date: 11/16/04
- Next message: Greg Fischer: "Re: Very large page - Need advice"
- Previous message: Mike Dole: "Re: UserControl - Textbox tag property looses value on postback"
- In reply to: Michael: "Asynchronous HttpWebRequest"
- Next in thread: Michael: "Re: Asynchronous HttpWebRequest"
- Reply: Michael: "Re: Asynchronous HttpWebRequest"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 16 Nov 2004 13:48:16 -0800
your approach will not work with a web app. you are starting a async request
then returning the page contents to the browser. later on the request
completes, and makes the callback, but because the page request is
completed, the call can no longer send data to the broswer (its not
listening). you have too options:
1) put a delay on the page until the async calls finishes (or switch to a
sync call).
2) or do what those other web sites do, start the call and return. but the
page uses a refresh (meta or javascript) to poll for the call to finish.
when finaly finished - redirect/transfer to a page that displays the results
of the call.
-- bruce (sqlwork.com)
"Michael" <xxx.xxx.xxx> wrote in message
news:%234RdaIBzEHA.2196@TK2MSFTNGP14.phx.gbl...
| Hi all..
|
| I need to submit an asynchronous request for a credit card authorization.
I
| have an aspx page where the user confirms the transaction and clicks a
| button to send the transaction to the authorization provider. When the
| submit button is clicked, I hide the panel displaying the details and
start
| a client-side progress bar using a literal control. This has to be an
async
| call because if I start the request in the button click synchronously,
| prerender doesn't get called until the request completes, so the progress
| bar is never displayed. When the request completes, I need to redirect to
a
| page used to display the authorization code, etc.
|
| I found an example of making an async request on MSDN and numerous other
| similar examples on the web. However, none are web apps, which I found
| curious. I was easy enough to incorporate the code into my web app - the
| async request works fine and I get a proper response.
|
| The problem is the redirect. It simply will not happen. Here is some
| pertinent code.
|
| Private Sub cmSubmit_Click()
| lbStatus.Text = "Processing Transaction. Please Wait..."
| pnConfirm.Visible = False
| pnSubmit.Visible = True
| startProcess.Text = "<script
language='jscript'>startProcess();</script>"
| 'asp:literal
| SendRequest()
| End Sub
|
| Private Sub SendRequest(ByVal PostData As String)
| b = oEnc.ASCII.GetBytes(PostData)
| oReq = CType(WebRequest.Create(URL), HttpWebRequest)
| oReq.Method = "POST"
| oReq.ContentType = "application/x-www-form-urlencoded"
| oReq.ContentLength = PostData.Length
| oState.Request = oReq
| oStr = oReq.GetRequestStream()
| oStr.Write(b, 0, b.Length)
| Dim r As IAsyncResult = CType(oReq.BeginGetResponse(New
| AsyncCallback(AddressOf RespCallback), oState), IAsyncResult)
| End Sub
|
| Public Sub RespCallback(ByVal ar As IAsyncResult)
| oState = CType(ar.AsyncState, RequestState)
| oReq = oState.Request
| oRes = CType(oReq.EndGetResponse(ar), HttpWebResponse)
| oStr = oRes.GetResponseStream
| oState.ResponseStream = oStr
| Dim iarRead As IAsyncResult = oStr.BeginRead(oState.ReadBuffer, 0,
| oState.BUFFER_SIZE, New AsyncCallback(AddressOf ReadCallback), oState)
| End Sub
|
| Public Sub ReadCallback(ByVal asyncResult As IAsyncResult)
| oState = CType(asyncResult.AsyncState, RequestState)
| oStr = oState.ResponseStream
| oRes = oState.Response
| Dim read As Integer = oStr.EndRead(asyncResult)
| If read > 0 Then
| Dim c(oState.BUFFER_SIZE) As Char
| Dim len As Integer = oState.StreamDecode.GetChars(oState.ReadBuffer,
0,
| read, c, 0)
| Dim str As String = New String(c, 0, len)
| oState.RequestData.Append(str)
| Dim ar As IAsyncResult = oStr.BeginRead(oState.ReadBuffer, 0,
| oState.BUFFER_SIZE, New AsyncCallback(AddressOf ReadCallback), oState)
| Else
| oStr.Close()
| oRes.Close()
| If oState.RequestData.Length > 1 Then Response.Redirect("status.aspx")
| 'I've also tried server.transfer with the same results
| End If
| End Sub
|
| I know this is possible; I see it every time I pay for anything online.
|
| Thanks for any insight..
|
| --
| Michael White
| Programmer/Analyst
| Marion County, OR
|
|
- Next message: Greg Fischer: "Re: Very large page - Need advice"
- Previous message: Mike Dole: "Re: UserControl - Textbox tag property looses value on postback"
- In reply to: Michael: "Asynchronous HttpWebRequest"
- Next in thread: Michael: "Re: Asynchronous HttpWebRequest"
- Reply: Michael: "Re: Asynchronous HttpWebRequest"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|