问题的结果传递给调用者异步HttpWebRequest的 [英] Problem to pass the result to the caller for asynchronous httpWebRequest

查看:141
本文介绍了问题的结果传递给调用者异步HttpWebRequest的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Silverlight中我希望能够得到来自Helper类的输出是这样的:

  MainPage的公()
    {
        的InitializeComponent();

        字符串输出中;
        助手帮手=新助手(URL);

        Helper.invoke(输出);

    }
 

我不明白怎么做,因为在辅助类,我有责任做一个异步调用:

 私人字符串webserviceUrl;
    私人XDOC的XDocument =新的XDocument();

    公共助手(字符串webserviceUrl)
    {
        this.webserviceUrl = webserviceUrl;
    }

    公共无效的invoke(REF字符串输出)
    {

        HttpWebRequest的HttpWebRequest的=(HttpWebRequest的)WebRequest.Create(this.webserviceUrl);
        尝试
        {
            HttpWebResponse httpWebResponse =(HttpWebResponse)httpWebRequest.BeginGetResponse(新的AsyncCallback(HandlerWebResponse),httpWebResponse);

        }
        抓住
        {

        }

  }

    私人无效HandlerWebResponse(IAsyncResult的asynchronousResult)
    {
        HttpWebRequest的要求=(HttpWebRequest的)asynchronousResult.AsyncState;
        HttpWebResponse响应=(HttpWebResponse)request.EndGetResponse(asynchronousResult);
        使用(StreamReader的streamReader1 =新的StreamReader(response.GetResponseStream()))
        {
            字符串resultString = streamReader1.ReadToEnd();

        }
    }
 

解决方案

创建事件通知该服务已成功使用。在事件参数可以传递的调用Web服务的结果。

 公共事件动作<字符串> ResponseResult;
 

您就可以在您的网络响应处理程序调用此事件:

 私人无效HandlerWebResponse(IAsyncResult的asynchronousResult)
{
    HttpWebRequest的要求=(HttpWebRequest的)asynchronousResult.AsyncState;
    HttpWebResponse响应=(HttpWebResponse)request.EndGetResponse(asynchronousResult);
    使用(StreamReader的streamReader1 =新的StreamReader(response.GetResponseStream()))
    {
        字符串resultString = streamReader1.ReadToEnd();
        如果(ResponseResult!= NULL)
             ResponseResult(resultString);

    }
}
 

和在$ C C同修的服务电话,您可以订阅该事件得到通知时,它已经完成$:

 辅助帮手=新助手(URL);
MainPage的公()
{
    的InitializeComponent();

    Helper.ResponseResult + = ResponseHandler的;
    Helper.invoke(输出);

}

公共无效ResponseHandler所(字符串响应)
{
    //做些事情的反应
}
 

In Silverlight I want to be able to get the output from a Helper class like this:

    public MainPage()
    {
        InitializeComponent();

        String ouput;
        Helper helper = new Helper(url);

        Helper.invoke(output);

    }

I can't see how to do that since in Helper Class I am obliged to do an asynchronous call:

    private String webserviceUrl;
    private XDocument xdoc = new XDocument();

    public Helper(String webserviceUrl)
    {
        this.webserviceUrl = webserviceUrl;
    }

    public void invoke(ref String output)
    {

        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(this.webserviceUrl);
        try
        {
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.BeginGetResponse(new AsyncCallback(HandlerWebResponse), httpWebResponse);

        }
        catch
        {

        }

  }

    private void HandlerWebResponse(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
        {
            string resultString = streamReader1.ReadToEnd();

        }
    }

解决方案

Create an event to notify that the service has been successfully consumed. In the event parameters you can pass the result of the invoked web services.

public event Action<string> ResponseResult;

You can then invoke this event in your web response handler:

private void HandlerWebResponse(IAsyncResult asynchronousResult)
{
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
    using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
    {
        string resultString = streamReader1.ReadToEnd();
        if (ResponseResult != null)
             ResponseResult(resultString);

    }
}

And in your code the initiates the service call you can subscribe to this event to get notified when it has finished:

Helper helper = new Helper(url);
public MainPage()
{
    InitializeComponent();

    Helper.ResponseResult += ResponseHandler;
    Helper.invoke(output);

}

public void ResponseHandler(string response)
{
    // do something with response
}

这篇关于问题的结果传递给调用者异步HttpWebRequest的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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