Response.Redirect在Async回调中无法正常工作

时间:2014-06-22 00:21:01

标签: c# .net asynchronous httpwebrequest response.redirect

如果这是一个副本,请提前抱歉,但我尝试了很多线程,但似乎没有任何解决方案可以帮助我。因此,我发布它的原因。

我正在使用异步 HttpWebRequest.BeginGetResponse 并且在回调函数中我调用的是Response.Redirect,它会抛出以下异常 在此上下文中无法获得响应

我曾尝试使用JavaScript来进行重定向,但这种方法效果不佳。以下是我的代码。

private void GET_PersonalDetails()
{
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);  
    RequestState myRequestState = new RequestState();
    myRequestState.request = myHttpWebRequest;
    myRequestState.setHeaders();

    IAsyncResult result = (IAsyncResult)myHttpWebRequest.BeginGetResponse(
                           new AsyncCallback(RespCallback), myRequestState);
}

private void RespCallback(IAsyncResult asynchronousResult)
{
    try
    {
        // State of request is asynchronous.
        RequestState myRequestState = (RequestState)asynchronousResult.AsyncState;
        HttpWebRequest myHttpWebRequest = myRequestState.request;
        myRequestState.response =
                (HttpWebResponse)myHttpWebRequest.EndGetResponse(asynchronousResult);

        // Read the response into a Stream object.
        string res = Utility.ReadResponse(myRequestState.response);

        Response.Redirect("decision.aspx");

        ClientScript.RegisterStartupScript(GetType(), "hwa", "alert('do something');", true);
        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "redirect();", true);
        return;
    }
    catch(WebException e){}

    }

0 个答案:

没有答案