与请求传递自定义值在BeginGetResponse回调使用 [英] Passing custom value with request to use in BeginGetResponse callback

查看:106
本文介绍了与请求传递自定义值在BeginGetResponse回调使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我需要发送一个Web请求的自定义值,所以我可以在我的应用程序接收响应使用它。由于我的应用程序可能会发送多个请求出我使用的是 BeginGetResponse 的每个请求异步发送出去。

目前,我缓存使用私有变量的值,然后在回调方法访问它。我知道这是自找麻烦,因为如果调用同样的方法之前,我已经得到了从previous请求发回一个响应,那么该数据可能已经改变了。

我需要知道什么是最好的(标准)的方式处理它的响应的请求来传递数据?我最初的想法是一个自定义标题,但我不知道...

下面是我的code在一分钟:

 公共无效SendRequestAsnyc(字符串参数1,字符串参数2,串URI,INT ID)
{
    NameValueCollection中@params = System.Web.HttpUtility.ParseQueryString(的String.Empty);
    @ params.Add(参数1参数1);
    @ params.Add(参数2,参数2);
    字节[]数据= UTF8Encoding.UTF8.GetBytes(@ params.ToString());
    HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(URI);
    request.Method = WebRequestMethods.Http.Post;
    request.ContentLength = data.Length;
    request.ContentType =应用/的X WWW的形式urlen codeD;
    VAR流= request.GetRequestStream();
    stream.Write(数据,0,data.Length);
    //采取一个音符数据中的回调被处理
    _cachedId = ID;
    IAsyncResult的AR = request.BeginGetResponse(新的AsyncCallback(GetAsyncResponse),请求);
}私人无效GetAsyncResponse(IAsyncResult的结果)
{
    HttpWebRequest的要求=(HttpWebRequest的)result.AsyncState;
    如果(request.HaveResponse)
    {
        VAR响应=(HttpWebResponse)request.EndGetResponse(结果);
        变种读者=新就是System.IO.StreamReader(response.GetResponseStream());
        VAR数据= reader.ReadToEnd();
        // 处理数据
        //使用_cachedId这里来更新数据库的响应
    }
}


解决方案

您需要创建一个包含要求一个新的对象 ID 。在C#previous版本,你可以做到这一点与对象的数组或可能一个匿名对象。这里是你如何与对象的数组做到这一点:

  IAsyncResult的AR = request.BeginGetResponse(GetAsyncResponse,新的对象[] {请求ID});私人无效GetAsyncResponse(IAsyncResult的结果)
{
    [对象] PARAMS =(对象[])result.AsyncState;
    HttpWebRequest的请求=(HttpWebRequest的)PARAMS [0];
    INT ID =(int)的PARAMS [1];
    //这里的过程
}

使用C#4.0中,你可以使用元组LT; HttpWebRequest的,诠释方式>

I have a situation where I need to send a custom value with a web request so I can use it in the response my application receives. As my application may be sending multiple requests out I am using BeginGetResponse to send out each request asynchronously.

At the moment I am caching the value using a private variable and then accessing it in the callback method. I know this is asking for trouble because if the same method is invoked before I have gotten a response back from the previous request then the data could have been changed.

What I need to know is what is the best (standard) way to pass data in a request to process it in the response? My initial thought was a custom header but I am not sure...

Here is my code at the minute:

public void SendRequestAsnyc(string param1, string param2, string uri, int id)
{
    NameValueCollection @params = System.Web.HttpUtility.ParseQueryString(String.Empty);
    @params.Add("param1", param1);
    @params.Add("param2", param2);          
    byte[] data = UTF8Encoding.UTF8.GetBytes(@params.ToString());
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);        
    request.Method = WebRequestMethods.Http.Post;
    request.ContentLength = data.Length;
    request.ContentType = "application/x-www-form-urlencoded";
    var stream = request.GetRequestStream();
    stream.Write(data, 0, data.Length);
    // take a note of data to be processed in the callback
    _cachedId = id;    
    IAsyncResult ar = request.BeginGetResponse(new AsyncCallback(GetAsyncResponse), request);
}

private void GetAsyncResponse(IAsyncResult result)
{
    HttpWebRequest request = (HttpWebRequest)result.AsyncState;
    if (request.HaveResponse)
    {                
        var response = (HttpWebResponse)request.EndGetResponse(result);
        var reader = new System.IO.StreamReader(response.GetResponseStream());
        var data = reader.ReadToEnd();
        // process data
        // use _cachedId here to update database with response
    }
}

解决方案

You need to create a new object that contains the request and the id. In previous versions of C#, you could do that with an array of objects or possibly an anonymous object. Here's how you would do it with an array of objects:

IAsyncResult ar = request.BeginGetResponse(GetAsyncResponse, new object[] {request, id});

private void GetAsyncResponse(IAsyncResult result)
{
    object[] params = (object[])result.AsyncState;
    HttpWebRequest request = (HttpWebRequest)params[0];
    int id = (int)params[1];
    // process here
}

With C# 4.0, you could use a Tuple<HttpWebRequest, int>.

这篇关于与请求传递自定义值在BeginGetResponse回调使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆