MNS访问令牌每次均会超时WebRequest.GetRequestStreamAsync()方法 [英] WebRequest.GetRequestStreamAsync() method timesout each time for MNS access token

查看:85
本文介绍了MNS访问令牌每次均会超时WebRequest.GetRequestStreamAsync()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从MNS获取用于Push通知的访问令牌,并且每次WebRequest.GetRequestStreamAsync()方法都超时.有什么建议吗?

参考: http://msdn.microsoft.com/zh-CN/library/windows/apps/hh913756.aspx

下面是我使用的代码

        HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create("https://login.live.com/accesstoken.srf");
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";

        string postString = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com",
                              SID,
                              SECRET_KEY);
        byte[] data = Encoding.UTF8.GetBytes(postString);
        Stream newStream = await webRequest.GetRequestStreamAsync();
        newStream.Write(data, 0, data.Length);

解决方案

我们遇到了同样的问题,结果发现问题出在与原先猜测不同的地方.

您需要.Dispose()或至少.Close()从.GetResponseAsync获得的响应,否则挂起对.GetRequestStreamAsync的下一个调用. /p>

似乎这背后的代码持有一定数量(相当低)的套接字或锁,从而不允许进一步的请求甚至开始,直到先前的请求完成为止.

I am trying to get the access token from MNS for Push notifications and the WebRequest.GetRequestStreamAsync() method timesout each time. Any suggestions?

Reference: http://msdn.microsoft.com/en-us/library/windows/apps/hh913756.aspx

Below is the code I use

        HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create("https://login.live.com/accesstoken.srf");
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";

        string postString = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com",
                              SID,
                              SECRET_KEY);
        byte[] data = Encoding.UTF8.GetBytes(postString);
        Stream newStream = await webRequest.GetRequestStreamAsync();
        newStream.Write(data, 0, data.Length);

解决方案

We had the same problem and it turned out to be a problem in different place than one would originally guess.

You need to .Dispose() or at least .Close() the response that you get from .GetResponseAsync, otherwise the next call to .GetRequestStreamAsync hangs.

It seems that the code behind this holds some limited (rather low) amount of sockets or locks, that disallow further requests to even begin until previous request has completed.

这篇关于MNS访问令牌每次均会超时WebRequest.GetRequestStreamAsync()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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