Asynchronous HttpWebRequest

M

Michael

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

bruce barker

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

Michael

Thanks for the reply bruce..

That's kinda what I expected after much thought into the event sequence, but
was just wondering if any more experienced that I with .NET knew any 'deep
dark secrets'.

Thanks again..
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top