473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Asynchronous HttpWebRequest

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
Nov 18 '05 #1
2 5264
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:%2****************@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
|
|
Nov 18 '05 #2
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

"bruce barker" <no***********@safeco.com> wrote in message
news:e1**************@TK2MSFTNGP12.phx.gbl...
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:%2****************@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
|
|

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sfoxover | last post by:
Hi, Could someone please give me some suggestions on how to make this class robust. I need to be able to handle around 20 similtanious requests to this class which causes a web browser to...
2
by: VBTricks.de.vu Webmaster | last post by:
Hello everybody, I need to write a HTTP client capable of downloading files using the HTTP protocoll. But downloading is one of my problems. I'm new to VB.net and I know there's a framework...
1
by: Assaf Shemesh | last post by:
Hi, I'm having a problem with asynchronous HttpWebRequest. It's a simple http client-server. For most of my users it works fine. However, some of them get the exception: ...
0
by: Raymondr | last post by:
Hi, First a brief description of out application: We have a webapplication which calls a couple of webservices during one request (postback). These calls to the webservices are made concurrent...
0
by: davidpenty | last post by:
Hi there, I am having some problems with a multi-threaded asp.net seach page. My search page sends off four asynchronous http requests to four search engines then waits for the results to come...
4
by: MaxMax | last post by:
I'm using HttpWebRequest. It seems that all the callback called from HttpWebRequest are in another thread (not in the "original" thread). Now my problem is that the "original" thread is the thread...
0
by: Morgan Cheng | last post by:
In the doc http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.timeout.aspx, it reads, "A Domain Name System (DNS) query may take up to 15 seconds to return or time out. If your...
7
by: Marc Bartsch | last post by:
Hi, I have a background worker in my C# app that makes a synchronous HttpWebRequest.GetResponse() call. The idea is to POST a file to a server on the internet. When I call HttpWebRequest.Abort()...
0
by: APA | last post by:
I've seen the MS sample async web request pattern and I ask is it really async if it is using a ManualResetEvent and setting WaitOne()? The ManualResetEvent object is being declared as a static...
1
by: sindhurasingeetham | last post by:
Hi, I'm new to coding in .NET. I am trying to do an asynchronous call in my code using httpwebrequest. This code works perfectly fine on one server, but does not work on another. The main flow...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.