WebRequest 没有 GetResponse 方法 - Windows Phone 8 [英] WebRequest has no GetResponse method - Windows Phone 8

查看:19
本文介绍了WebRequest 没有 GetResponse 方法 - Windows Phone 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向 API 发送一个带有他们要求的参数的 post 请求......我最终只是创建了一个字符串,这很丑陋,但我不知道如何让它以不同的方式工作.然后我发现这个 WebRequest 类有很多变体,但不幸的是我无法让它工作.

I want to send a post request to an API with some parameters they ask for... I ended up just creating a string, it's ugly but I do not know a way of making it work differently. I then found lots of variations on this WebRequest class, but unfortunately I cannot get it to work.

主要问题可能是因为我并没有真正理解这一切是如何组合在一起的,但基本上,我一直在关注的示例使用 WebRequest 方法 GetResponse ......甚至在 MSDN 上它有这个,所以我想知道为什么当我尝试在我的代码中调用它时,我没有得到那个选择?GetRequestStream 也是如此.

Main problem is probably because I am not really understanding how this is all fitting together but basically, the examples I have been following use WebRequest method GetResponse... even on MSDN it has this, so I am wondering why when I try to call it in my code, I am not getting that choice? Same goes for GetRequestStream.

如何向 WebRequest 添加参数?

        *****DBContext()
        {
            data = "grant_type=" + GRANTTYPE + "&username=" + username + "&password=" + password + "&client_id=" + CLIENTID + "&redirect_uri=" + REDIRECTURI + "&client_secret=" + CLIENTSECRET;
        }

        public bool Authenticate()
        {   
            byte[] dataStream = Encoding.UTF8.GetBytes(data);
            WebRequest webRequest = WebRequest.Create(urlPath);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/json";
            webRequest.ContentLength = dataStream.Length;  
            Stream newStream = webRequest.GetRequestStream();
            // Send the data.
            newStream.Write(dataStream, 0, dataStream.Length);
            newStream.Close();
            WebResponse webResponse = webRequest.GetResponse();

            return true;
        }

我也有一个问题,当我最终让这些东西工作时,我应该在回调 uri 中放入什么.如果是电话,是否在本地主机上运行?

I also have the question of when I finally do get this stuff to work, what should I be putting in the callback uri. if it's a phone, is it running off of localhost?

推荐答案

Windows Phone 的 .NET 编译包含 WebRequest 类的实现,该类没有用于获取请求流和响应的同步方法,因为这些会阻止在 UI 线程上执行,直到操作完成.您可以将现有的 Begin/End 方法直接与回调委托一起使用,或者您可以将这些调用包装在异步扩展中,这将为您提供您习惯的可读性和功能(或多或少).我的首选方法是定义扩展,因此我将演示此方法,但与回调模式相比,它没有性能优势.它确实具有在您需要使用 WebRequest 时易于移植的优点.

The .NET compilation for Windows Phone contains an implementation of the WebRequest class which does not have synchronous methods for obtaining request stream and response, as these would block execution on the UI thread until the operations completed. You can use the existing Begin/End methods directly with callback delegates, or you can wrap those calls in async extensions that will give you the kind of readability and functionality you're used to (more or less). My preferred method is defining extensions, so I will demonstrate this method, but it has no performance advantage over the callback pattern. It does have the up-side of being easily portable any time you need to make use of a WebRequest.

异步/等待模式

为 WebRequest 类定义自定义扩展:

Define custom extensions for the WebRequest class:

public static class Extensions
{
    public static System.Threading.Tasks.Task<System.IO.Stream> GetRequestStreamAsync(this System.Net.WebRequest wr)
    {
        if (wr.ContentLength < 0)
        {
            throw new InvalidOperationException("The ContentLength property of the WebRequest must first be set to the length of the content to be written to the stream.");
        }

        return Task<System.IO.Stream>.Factory.FromAsync(wr.BeginGetRequestStream, wr.EndGetRequestStream, null);
    }

    public static System.Threading.Tasks.Task<System.Net.WebResponse> GetResponseAsync(this System.Net.WebRequest wr)
    {
        return Task<System.Net.WebResponse>.Factory.FromAsync(wr.BeginGetResponse, wr.EndGetResponse, null);
    }
}

使用新的扩展(确保导入定义静态扩展类的命名空间):

Use the new extensions (be sure to import the namespace where your static Extensions class was defined):

public async System.Threading.Tasks.Task<bool> AuthenticateAsync()
{
    byte[] dataStream = System.Text.Encoding.UTF8.GetBytes("...");
    System.Net.WebRequest webRequest = System.Net.WebRequest.Create("...");
    webRequest.Method = "POST";
    webRequest.ContentType = "application/json";
    webRequest.ContentLength = dataStream.Length;
    Stream newStream = await webRequest.GetRequestStreamAsync();
    // Send the data.
    newStream.Write(dataStream, 0, dataStream.Length);
    newStream.Close();
    var webResponse = await webRequest.GetResponseAsync();

    return true;
}

关于你的最后一点,目前我没有看到足够的信息来理解回调 URI 是什么、它的定义位置以及它如何影响你正在做的事情.

Regarding your final note, at the moment I don't see enough information to make sense of what the callback URI is, where it's defined, and how it affects what you're doing.

这篇关于WebRequest 没有 GetResponse 方法 - Windows Phone 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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