开发者

About HttpWebRequest's EndGetResponse

开发者 https://www.devze.com 2023-04-04 09:05 出处:网络
I\'m using asynchronous calls for communication to my server. I written some component to collect all unauthorized requests and to resend them after user logs in. I written some test to produce 10 thr

I'm using asynchronous calls for communication to my server. I written some component to collect all unauthorized requests and to resend them after user logs in. I written some test to produce 10 threads that are sending some requests without first being authorized. Than I wait for 20 seconds and do authorization and after that I wait for request to successfully finish. But problem appeared at EndGetResponse method which I call in my callback method. I done that this way:

public void InternalCallback(IAsyncResult result)
{
    try
    {
        RequestState state = (RequestState)result.AsyncState;
        IHttpWebRequest request = state.Request;    

        using (IHttpWebResponse response = responseGetter.GetResponse(request, result))
        {
           // ...
        }
     }
     // ...
}

So, I made some custom class RequestState which has some higher level callbacks I need and it has request which I'll use to call EndGetResponse method. But this way I got error:

IAsyncResult object was not returned from the corresponding asynchronous method.

I changed this so I now have Request field in my callback class which I set before calling BeginGetResponse and I use that Request field when calling EndGetResponse in my callback.

public void InternalCallback(IAsyncResult result)
{
    try
    {                                   
 开发者_如何学Python       using (IHttpWebResponse response = responseGetter.GetResponse(this.Request, result))
        {
           // ...
        }
     }
     // ...
}

Is this new solution valid one? Can you suggest is this good way to do this or how should I do this?

0

精彩评论

暂无评论...
验证码 换一张
取 消