处理HttpWebRequest Url大小问题

时间:2015-03-16 09:27:41

标签: c# windows-phone-8.1 win-universal-app

我正在使用C#和Windows Phone 8.1(作为通用应用)。我正在尝试使用此代码获取大小:

    async public void LoadInformation(string url)
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
            request.Method = "HEAD";
            HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
            //var response = await request.GetResponseAsync();

            if (response == null)
                return;
            string pageSource = new StreamReader(response.GetResponseStream()).ReadToEnd();
            string Size = CalculateBytes(response.ContentLength.ToString());

            var dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;
            await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                string title = txtTitle.Text;
                txtTitle.Text = title + " | " + Size;
            });
        }
        catch (Exception ex) { Debug.WriteLine("LoadInformationError:\t" + ex.Message); }
    }

在某些网址中,它会返回错误:

Additional information: The remote server returned an error: NotFound.

但该URL是正确的,可以通过Internet Explorer下载。截屏:

http://i.stack.imgur.com/RU51s.png

我在多个UserControls中同时多次使用此方法(每1个UserControl加载大小只有1次),您认为这是原因吗?

在例外的调试菜单中,我检查了“公共语言运行时异常”,以查找不是此问题的其他问题。截屏:

http://i.stack.imgur.com/wWInX.png

0 个答案:

没有答案