WebRequest不包含Windows 10 Universal App的'GetResponse'定义 [英] WebRequest does not contain a definition for 'GetResponse' for Windows 10 Universal App

查看:215
本文介绍了WebRequest不包含Windows 10 Universal App的'GetResponse'定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GitHub上下载了一个控制台应用程序,并且运行良好. 但是我想将此C#控制台应用程序转换为通用Windows 10应用程序.

I download a Console Application on GitHub and works fine. But I want to translate this C# Console Application to a Universal Windows 10 Application.

Visual Studio上的错误:Web Request does not contain a definition for...

这是代码:

        private AccessTokenInfo HttpPost(string accessUri, string requestDetails)
    {

        //Prepare OAuth request 
        WebRequest webRequest = WebRequest.Create(accessUri);
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.Method = "POST";
        byte[] bytes = Encoding.ASCII.GetBytes(requestDetails);

        webRequest.ContentLength = bytes.Length;
        using (Stream outputStream = webRequest.GetRequestStream())
        {
            outputStream.Write(bytes, 0, bytes.Length);
        }
        using (WebResponse webResponse = webRequest.GetResponse())
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AccessTokenInfo));
            //Get deserialized object from JSON stream
            AccessTokenInfo token = (AccessTokenInfo)serializer.ReadObject(webResponse.GetResponseStream());
            return token;
        }
    }

基本上我在这3个函数上遇到错误:

basically I get an error on this 3 functions:

webRequest.ContentLength
webRequest.GetRequestStream()
webRequest.GetResponse()

这是错误的图片: 错误的图片:不包含定义"

附加评论: 我在GitHub上阅读了一些类似的问题,似乎我需要创建一个AsyncResult,例如

Aditional Comments: I read a few similar problems on GitHub and it seems I need to create a AsyncResult like this

这是一些类似问题的答案(我不知道如何将其应用于我的问题):

  /**  In case it is a Windows Store App (and not WPF) there is no synchronous GetResponse method in class WebResponse.

 You have to use the asynchronous GetResponseAsync instead, e.g. like this: **/

using (var response = (HttpWebResponse)(await request.GetResponseAsync()))
{
    // ...
}

提前谢谢!

推荐答案

在您链接的答案中,有一个带有实际答案的评论:

in the answer you linked there is a comment with the actual answer:

如果它是Windows Store应用程序(而不是WPF),则没有 WebResponse类中的同步GetResponse方法

In case it is a Windows Store App (and not WPF) there is no synchronous GetResponse method in class WebResponse

您必须在UWP应用中使用异步方法,因为它们不再具有同步校准来提高用户界面性能(不再挂起UI,因为它在同一线程上执行Web请求)

you have to use the async methods in UWP apps since they don't have synchronous cals anymore to improve user interface performance (no more hanging UI because it's doing a web request on the same thread)

您的代码应采用

HttpWebResponse response = await webrequest.GetResponseAsync();

以下是有关异步/等待以及如何使用它的更多信息: https ://msdn.microsoft.com/zh-CN/library/hh191443.aspx

Here is some more info on async/await and how to use it: https://msdn.microsoft.com/en-us/library/hh191443.aspx

这篇关于WebRequest不包含Windows 10 Universal App的'GetResponse'定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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