简体   繁体   English

使用webclient时如何在OpenReadCompletedEvent中获取URL

[英]How can I get the URL in OpenReadCompletedEvent when I use webclient

How can I get the URL in OpenReadCompletedEvent when I use webclient. 使用webclient时,如何在OpenReadCompletedEvent中获取URL。

 WebClient webClient = new WebClient();
 webClient.OpenReadAsync(url);       // in event method I want get this url       
 webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(CardInfoDown_Completed);   


 private void CardInfoDown_Completed(object sender, OpenReadCompletedEventArgs e)
    {
        if (e.Error == null)
        {           
            using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Result))
            {
                // I want get the url here,
                // How to do this?
                string strStream = reader.ReadToEnd();              
            }
        }          
    }

Thank you! 谢谢!

    WebClient webClient = new WebClient();
    webClient.BaseAddress = "http://hhh.com";
    webClient.OpenReadAsync(new Uri("http://hhh.com"));       // in event method I want get this url       
    webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(CardInfoDown_Completed);

And: 和:

private void CardInfoDown_Completed(object sender, OpenReadCompletedEventArgs e)
{
    if (e.Error == null)
    {
        using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Result))
        {
            // I want get the url here,
            // How to do this?
            var client = sender as WebClient;
            if (client != null)
            {
                var url = client.BaseAddress; //returns hhh.com
            }
            string strStream = reader.ReadToEnd();
        }
    }

Anton Sizikov's solution is good, but will only work if the URL is absolute (like http://hhh.com ). 安东·西齐科夫(Anton Sizikov)的解决方案很好,但是仅在URL是绝对的(例如http://hhh.com )时才可以使用。 If using a relative URL, .NET will automatically merge the base address with the relative URL (therefore potentially resulting in an invalid URL). 如果使用相对URL,.NET将自动将基本地址与相对URL合并(因此可能导致无效的URL)。

To send a value to the OpenReadCompleted event handler, you're supposed to use this OpenRead overload to provide a custom token (in this case, your URL): http://msdn.microsoft.com/en-us/library/ms144212(v=vs.95).aspx 要将值发送到OpenReadCompleted事件处理程序,您应该使用此OpenRead重载来提供自定义标记(在本例中为URL): http : //msdn.microsoft.com/zh-cn/library/ms144212 (v = VS.95)的.aspx

WebClient webClient = new WebClient();
webClient.OpenReadAsync(new Uri("http://hhh.com"), "http://hhh.com");
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(CardInfoDown_Completed);

Then to retrieve the value: 然后检索值:

private void CardInfoDown_Completed(object sender, OpenReadCompletedEventArgs e)
{
    if (e.Error == null)
    {
        using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Result))
        {
            var url = (string)e.UserState;
            string strStream = reader.ReadToEnd();
        }
    }
}

For me even a simpler variation from the above works fine 对我来说,即使是上述方法的更简单的变化也可以

    private void CardInfoDown_Completed(object sender, DownloadStringCompletedEventArgs e)
    {
        string url;

        if (e.Error == null)
        {
            url  = (string)e.UserState;
        }
       // ...
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WebClient.DownloadString(url)当这个url返回404页面时,我怎么能跳过这个? - WebClient.DownloadString(url) when this url returns a 404 page, how can i skip this? 如何从安全的URL(https)使用WebClient.DownloadString? - How can I use WebClient.DownloadString from a secure URL (https)? 在webclient.ResponseHeaders之后使用webclient.DownloadFile时出现WebException超时 - I get WebException timeout when I use webclient.DownloadFile after webclient.ResponseHeaders 如何让WebClient(Web服务客户端)自动使用默认代理服务器? - How can I get WebClient (webservice client) to automatically use the default proxy server? 如何使用WebClient从网页获取异步发布数据? - How can I get asynchronous post data from a webpage with WebClient? 我可以使用webclient.uploadvalues() - can I use webclient.uploadvalues() 当我使用.NET WebClient DownloadFileAsync时,我随机返回零长度的文件 - When I use the .NET WebClient DownloadFileAsync I randomly get zero length files returned 为什么将WebClient.DownloadData用作URL时会出现异常? - Why do I get an exception when using WebClient.DownloadData for a url? 当我将WebClient与Silverlight 4.0一起使用时无法执行Response.Redirect - Can not do Response.Redirect when I use WebClient with Silverlight 4.0 to call aspx page 如何调试此Webclient异常? - How can I debug this webclient exception?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM