【问题标题】:WebRequest has no GetResponse method - Windows Phone 8WebRequest 没有 GetResponse 方法 - Windows Phone 8
【发布时间】:2013-09-15 20:09:06
【问题描述】:

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

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

How to add parameters into a 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 中放入什么。如果是电话,它会在本地主机上运行吗?

【问题讨论】:

  • 虽然还是一样的编译错误。
  • 不确定您的问题是什么。您是否尝试解析响应?
  • 我第一次编写代码以使用 C# 向外部网站的 API 发送发布请求。该 API 将返回一个 json 字符串。 developers.podio.com/authentication/username_password 。不行,是说webRequest没有GetResponse或者GetRequestSream方法

标签: c# windows-phone-8 webrequest


【解决方案1】:

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

异步/等待模式

为 WebRequest 类定义自定义扩展:

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);
    }
}

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

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 是什么、它在哪里定义以及它如何影响您正在做的事情。

【讨论】:

  • Microsoft.Bcl.Async nuget 包已经有很多 async/await 扩展方法(包括用于 HttpWebRequest 的扩展方法)
  • 这个问题与那个包无关。
  • 那是对您的回答的评论而不是问题,只是说创建与 Microsoft 已经创建的完全相同的扩展方法是没有用的:)
  • 老实说,我不想在不必要的时候拉入整个库,尤其是因为我无法控制它的功能。我不认为可以通过简单的扩展来解决的问题值得图书馆参考。
  • GetRequestStreamAsync() 未找到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多