473,804 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Visib le = False
pnSubmit.Visibl e = True
startProcess.Te xt = "<script language='jscri pt'>startProces s();</script>"
'asp:literal
SendRequest()
End Sub

Private Sub SendRequest(ByV al PostData As String)
b = oEnc.ASCII.GetB ytes(PostData)
oReq = CType(WebReques t.Create(URL), HttpWebRequest)
oReq.Method = "POST"
oReq.ContentTyp e = "applicatio n/x-www-form-urlencoded"
oReq.ContentLen gth = PostData.Length
oState.Request = oReq
oStr = oReq.GetRequest Stream()
oStr.Write(b, 0, b.Length)
Dim r As IAsyncResult = CType(oReq.Begi nGetResponse(Ne w
AsyncCallback(A ddressOf RespCallback), oState), IAsyncResult)
End Sub

Public Sub RespCallback(By Val ar As IAsyncResult)
oState = CType(ar.AsyncS tate, RequestState)
oReq = oState.Request
oRes = CType(oReq.EndG etResponse(ar), HttpWebResponse )
oStr = oRes.GetRespons eStream
oState.Response Stream = oStr
Dim iarRead As IAsyncResult = oStr.BeginRead( oState.ReadBuff er, 0,
oState.BUFFER_S IZE, New AsyncCallback(A ddressOf ReadCallback), oState)
End Sub

Public Sub ReadCallback(By Val asyncResult As IAsyncResult)
oState = CType(asyncResu lt.AsyncState, RequestState)
oStr = oState.Response Stream
oRes = oState.Response
Dim read As Integer = oStr.EndRead(as yncResult)
If read > 0 Then
Dim c(oState.BUFFER _SIZE) As Char
Dim len As Integer = oState.StreamDe code.GetChars(o State.ReadBuffe r, 0,
read, c, 0)
Dim str As String = New String(c, 0, len)
oState.RequestD ata.Append(str)
Dim ar As IAsyncResult = oStr.BeginRead( oState.ReadBuff er, 0,
oState.BUFFER_S IZE, New AsyncCallback(A ddressOf ReadCallback), oState)
Else
oStr.Close()
oRes.Close()
If oState.RequestD ata.Length > 1 Then Response.Redire ct("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 5306
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******** ********@TK2MSF TNGP14.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.Visib le = False
| pnSubmit.Visibl e = True
| startProcess.Te xt = "<script
language='jscri pt'>startProces s();</script>"
| 'asp:literal
| SendRequest()
| End Sub
|
| Private Sub SendRequest(ByV al PostData As String)
| b = oEnc.ASCII.GetB ytes(PostData)
| oReq = CType(WebReques t.Create(URL), HttpWebRequest)
| oReq.Method = "POST"
| oReq.ContentTyp e = "applicatio n/x-www-form-urlencoded"
| oReq.ContentLen gth = PostData.Length
| oState.Request = oReq
| oStr = oReq.GetRequest Stream()
| oStr.Write(b, 0, b.Length)
| Dim r As IAsyncResult = CType(oReq.Begi nGetResponse(Ne w
| AsyncCallback(A ddressOf RespCallback), oState), IAsyncResult)
| End Sub
|
| Public Sub RespCallback(By Val ar As IAsyncResult)
| oState = CType(ar.AsyncS tate, RequestState)
| oReq = oState.Request
| oRes = CType(oReq.EndG etResponse(ar), HttpWebResponse )
| oStr = oRes.GetRespons eStream
| oState.Response Stream = oStr
| Dim iarRead As IAsyncResult = oStr.BeginRead( oState.ReadBuff er, 0,
| oState.BUFFER_S IZE, New AsyncCallback(A ddressOf ReadCallback), oState)
| End Sub
|
| Public Sub ReadCallback(By Val asyncResult As IAsyncResult)
| oState = CType(asyncResu lt.AsyncState, RequestState)
| oStr = oState.Response Stream
| oRes = oState.Response
| Dim read As Integer = oStr.EndRead(as yncResult)
| If read > 0 Then
| Dim c(oState.BUFFER _SIZE) As Char
| Dim len As Integer = oState.StreamDe code.GetChars(o State.ReadBuffe r,
0,
| read, c, 0)
| Dim str As String = New String(c, 0, len)
| oState.RequestD ata.Append(str)
| Dim ar As IAsyncResult = oStr.BeginRead( oState.ReadBuff er, 0,
| oState.BUFFER_S IZE, New AsyncCallback(A ddressOf ReadCallback), oState)
| Else
| oStr.Close()
| oRes.Close()
| If oState.RequestD ata.Length > 1 Then Response.Redire ct("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******** ******@TK2MSFTN GP12.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******** ********@TK2MSF TNGP14.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.Visib le = False
| pnSubmit.Visibl e = True
| startProcess.Te xt = "<script
language='jscri pt'>startProces s();</script>"
| 'asp:literal
| SendRequest()
| End Sub
|
| Private Sub SendRequest(ByV al PostData As String)
| b = oEnc.ASCII.GetB ytes(PostData)
| oReq = CType(WebReques t.Create(URL), HttpWebRequest)
| oReq.Method = "POST"
| oReq.ContentTyp e = "applicatio n/x-www-form-urlencoded"
| oReq.ContentLen gth = PostData.Length
| oState.Request = oReq
| oStr = oReq.GetRequest Stream()
| oStr.Write(b, 0, b.Length)
| Dim r As IAsyncResult = CType(oReq.Begi nGetResponse(Ne w
| AsyncCallback(A ddressOf RespCallback), oState), IAsyncResult)
| End Sub
|
| Public Sub RespCallback(By Val ar As IAsyncResult)
| oState = CType(ar.AsyncS tate, RequestState)
| oReq = oState.Request
| oRes = CType(oReq.EndG etResponse(ar), HttpWebResponse )
| oStr = oRes.GetRespons eStream
| oState.Response Stream = oStr
| Dim iarRead As IAsyncResult = oStr.BeginRead( oState.ReadBuff er, 0,
| oState.BUFFER_S IZE, New AsyncCallback(A ddressOf ReadCallback), oState)
| End Sub
|
| Public Sub ReadCallback(By Val asyncResult As IAsyncResult)
| oState = CType(asyncResu lt.AsyncState, RequestState)
| oStr = oState.Response Stream
| oRes = oState.Response
| Dim read As Integer = oStr.EndRead(as yncResult)
| If read > 0 Then
| Dim c(oState.BUFFER _SIZE) As Char
| Dim len As Integer = oState.StreamDe code.GetChars(o State.ReadBuffe r,
0,
| read, c, 0)
| Dim str As String = New String(c, 0, len)
| oState.RequestD ata.Append(str)
| Dim ar As IAsyncResult = oStr.BeginRead( oState.ReadBuff er, 0,
| oState.BUFFER_S IZE, New AsyncCallback(A ddressOf ReadCallback), oState)
| Else
| oStr.Close()
| oRes.Close()
| If oState.RequestD ata.Length > 1 Then Response.Redire ct("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
2613
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 display a waiting message while it requests XML data from a third party server. Some requests can take around 30 seconds. Usually the first time I do the request it always works. If I try again without closing the web browser it will fail sometimes....
2
1784
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 offering a solution for almost every problem. But for me it's a bit complex. So what I need: I need a way to download files asynchronously. In VB6 I solved this problem with a self-written component using sockets. As of the large
1
3373
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: System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: The attempted operation is not supported for the type of object referenced at System.Net.Sockets.Socket.get_ConnectEx()
0
1626
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 using asynchronous webservices calls. The number of webservices called concurrent is between 1 and 18. The webservice calls are made using SSL with a X509 clientcertificate. The application is underhigh load
0
1107
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 back (in xml) The searches are just URLs with querystrings. Each request sets a boolean to true when it has got a reply from the search engine. I have declare the booleans as 'volatile'.
4
4549
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 that maintains the interface (the forms). I want to signal from the "worker" thread to the main thread that I've finished downloading the page. How should I do? I can't simply wait for the end of the thread as in this...
0
5149
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 request contains a host name that requires resolution and you set Timeout to a value less than 15 seconds, it may take 15 seconds or more before a WebException is thrown to indicate a timeout on your request." The words doesn't appear in .Net 1.1...
7
7692
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() on the request object on another thread, GetResponse() returns with an exception as expected. However, when I monitor the network traffic, it does not seem to stop, but to continue to be active and to upload the file. The network is active even...
0
254
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 variable so isn't it causing problems with other threads that may be using the same class to execute the async web request? If I remove the ManualResetEvent object will it be truly asynchronous and will I be losing something? Also, in my app the web...
1
1888
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 works on the new server, but it somehow does not do the asynchronous calling part. I'm unable to see the async part hitting IIS (from the IIS logs) as well. I have compared the IIS settings on both the servers and they are the same. The settings on both...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.