Re: Asynchronous HttpWebRequest

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: bruce barker (nospam_brubar_at_safeco.com)
Date: 11/16/04


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



Relevant Pages

  • Re: Asynchronous HttpWebRequest
    ... > your approach will not work with a web app. ... later on the request ... > | async request works fine and I get a proper response. ... > | End Sub ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: upload 50M file to web service got 500 internal server error
    ... Try looking at the response in the trace. ... if the trace never shows a request, ... Right now maxRequestLength property of Web App and Webservcie are all set to ... But in trace.axd of the Webservice, ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: HTML email with hidden image request for website statistics gathering?
    ... Websphere and a J2EE web app. ... This web app produces an HTML email ... mean they read it, but opened it, and the request is ... But won't the proxy server end up caching the image after the 1st or ...
    (comp.lang.java.programmer)
  • 401 responses with ntlm enabled
    ... I'm using iis6 and my web app is configured for integrated windows ... However, looking at the IIS log, every HTTP request gets sent 3 times. ... third time the cs-username field is correct and the response is 200. ...
    (microsoft.public.inetserver.iis.security)
  • Re: [PHP] displaying image from MySQL DB using HTML/PHP
    ... On Tue, March 13, 2007 9:07 pm, Jim Lucas wrote: ... I'm just suggesting that not using REQUEST is not an absolute. ... But if you have a web app that works equally well for other pages to ... link to, or to send POST data to, using REQUEST is quite nice, imho. ...
    (php.general)