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

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

问题描述

我想发个帖子请求到API的一些参数,他们要求......最后我只创建一个字符串,它的丑陋,但我不知道使其工作不同的方式。后来我发现很多在这个变化的的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 不具有获取请求流和响应同步方法,如直到操作完成了这些会阻止用户界面线程上执行类。您可以直接与回调委托使用现有的开始/结束的方法,或者你可以用在异步扩展这些电话会给你那种你已经习惯了(或多或少)的可读性和功能。我的首选方法是定义扩展,所以我会证明这一点的方法,但它拥有的回调格局没有性能优势。它具有易于携带,你需要利用任何时间向上侧的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天全站免登陆