httpWebRequest.GetResponseAsync永远不会返回

时间:2014-05-01 15:19:27

标签: windows windows-8.1

我正在开发一个通用应用,我在Windows 8.1.1上遇到了问题。

该代码适用于Windows Phone 8.1,但不适用于Windows 8.1.1。 在Windows 8.1.1上,方法httpWebRequest.GetResponseAsync永远不会返回..

这是我的代码:

    public async Task<StorageFile> DownloadAsync(String url)
    {
        String fileName = url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Last();
        StorageFile storageFile = await this.DoesFileExistAsync(fileName);

        try
        {
            if (storageFile == null)
            {
                if (InternetHelper.IsConnectedToInternet())
                {
                    String login = "login";
                    String password = "pass";
                    await this.ConnectAsync(login, password);

                    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                    httpWebRequest.Method = "GET";
                    httpWebRequest.CookieContainer = this._container;
                    HttpWebResponse httpWebResponse = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
                    HttpStatusCode code = httpWebResponse.StatusCode;
                    if (code != HttpStatusCode.OK)
                    {
                        return (null);
                    }
                    storageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
                    using (Stream fs = await storageFile.OpenStreamForWriteAsync())
                    using (Stream content = httpWebResponse.GetResponseStream())
                    using (MemoryStream ms = new MemoryStream())
                    {
                        content.CopyTo(ms);
                        fs.Seek(0, SeekOrigin.Begin);
                        ms.WriteTo(fs);
                    }
                    await this.DisconnectAsync();
                }
            }
        }
        catch (Exception e)
        {
            this.NotifyError(e.Message, url, "-");
        }
        return (storageFile);
    }

调用上一个方法的方法是:

    private async void ExecuteShowFileCommand(String link)
    {
        StorageFile file = await WebSiteParser.Instance.DownloadAsync(link);
        if (file != null)
            await Windows.System.Launcher.LaunchFileAsync(file);
    }

有没有人有想法?

0 个答案:

没有答案