【问题标题】:Get application to wait until Variables are updated让应用程序等到变量更新
【发布时间】:2014-01-15 22:17:42
【问题描述】:

我目前正在使用 OAuth。当前代码的问题在于它不会等到用户允许站点上的应用程序并获得正确的密钥和秘密。我使用的是线程类型等待,但有时它不够长......有些用户比其他用户慢。我附上了我的代码的 sn-p。我想知道在哪里插入一个while语句,或者我应该使用它吗?

public OAuthToken GetRequestToken(Uri baseUri, string consumerKey, string consumerSecret)
{
    var uri = new Uri(baseUri, "oauth/request_token");
    uri = SignRequest(uri, consumerKey, consumerSecret);
    var request = (HttpWebRequest) WebRequest.Create(uri);
    request.Method = WebRequestMethods.Http.Get;
    var response = request.GetResponse();
    var queryString = new StreamReader(response.GetResponseStream()).ReadToEnd();
    var parts = queryString.Split('&');
    var token = parts[1].Substring(parts[1].IndexOf('=') + 1);
    var secret = parts[0].Substring(parts[0].IndexOf('=') + 1);
    return new OAuthToken(token, secret);
} 

【问题讨论】:

  • 您使用的是什么 OAuth 库?
  • 您能否说明您需要等待的原因?这是一个什么样的应用程序?网页、桌面?根据应用程序的类型,您可以采用许多不同的方法
  • @klugerama 据我所知,我相信它是 Dropbox REST API
  • @Jim 它在哪里说它是 Dropbox REST API?
  • @Leo Nowhere,我只是在猜测DropBoxPart5 Oauth ;)

标签: c# oauth


【解决方案1】:

您现在应该切换到 .NET 附带的较新的 System.Net.HttpSystem.Net.Http.WebRequest 库。这些都使用 .NET 4.5 提供的新异步编程材料。

您可以调用请求(返回您可以等待的任务对象)并自动暂停线程以进行响应。 UI 不会像往常一样响应。如果您不了解新的 async 和 await 关键字的工作原理,这可能是最简单的做法。有关它们的更多信息,请参阅http://msdn.microsoft.com/en-us/library/hh191443.aspx

这是您使用新库执行操作的代码:

using System.Net.Http;

public OAuthToken GetRequestToken(Uri baseUri, string consumerKey, string consumerSecret)
{
    var uri = new Uri(baseUri, "oauth/request_token");
    uri = SignRequest(uri, consumerKey, consumerSecret);

    var message = new HttpRequestMessage(new HttpMethod("GET"), uri);
    var handler = new WebRequestHandler();
    var client = new HttpClient(handler);

    // Use the http client to send the request to the server.
    Task<HttpResponseMessage> responseTask = client.SendAsync(message);

    // The responseTask object is like a wrapper for the other task thread.
    // We can tell this task object that we want to pause our current thread
    // and wait for the client.SendAsync call to finish.
    responseTask.Wait();

    // - Once that thread finishes, and the code continues on, we need to 
    //   tell it to read out the response data from the backing objects. 
    // - The responseTask.Result property represents the object the async task 
    //   was wrapping, we want to pull it out, then use it and get the content 
    //   (body of the response) back. 
    // - Getting the response actually creates another async task (the 
    //   .ReadAsStringAsync() call) but by accessing the .Result
    //   property, it is as if we called .ReadAsStringAsync().Wait(); Except that
    //   by using Result directly, we not only call Wait() but we get the resulting, 
    //   wrapped object back. Hope that didn't confuse you much :)
    var queryString = responseTask.Result.Content.ReadAsStringAsync().Result;

    // And all your other normal code continues.
    var parts = queryString.Split('&');
    var token = parts[1].Substring(parts[1].IndexOf('=') + 1);
    var secret = parts[0].Substring(parts[0].IndexOf('=') + 1);
    return new OAuthToken(token, secret);
} 

【讨论】:

    【解决方案2】:

    为什么不使用模态弹窗然后在提交按钮上调用认证类

    【讨论】:

      猜你喜欢
      • 2011-04-07
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 2017-06-24
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多