开发者

DotNetOpenAuth and Async WebRequest (BeginGetResponse)

开发者 https://www.devze.com 2023-01-30 17:09 出处:网络
DotNetOpenAuth Samples don\'t include an example for performing a call asynchronously. Here\'s my implementation. I\'d like to know if I\'m doing something wrong.

DotNetOpenAuth Samples don't include an example for performing a call asynchronously. Here's my implementation. I'd like to know if I'm doing something wrong.

public static IAsyncResult BeginGetFromGoogle(string accessToken)
{
    var oauthConsumer = new WebConsumer(ServiceDescription, TokenManager);
    var request = oauthConsumer.PrepareAuthorizedRequest(googleEndpoint, accessToken, OAuthOpenIdHelper.MakeParameters("key", googleApiKey));
    return request.BeginGetResponse(new AsyncCallback(endGetFromGoogle), new GetFromGoogleContext { Request = request });
}

protected static void endGetFromGoogle(IAsyncResult result)
{
    var context = (GetFromGoogleContext)result.AsyncState;
    WebResponse response = context.Request.EndResponse(result);
    string body = new StreamReader(response.GetResponseStream()).ReadToEnd();
    var serializer = new JavaScriptSerializer();
    // serializer.Deserialize<something>(); // do something with the data
}

protected class GetFromGoogleContext
{
    public HttpWebRequest Request;
}

To be clear, this is working fine for me, but the implementation in the Samples uses a couple further classes that don't accomplish any clear result to me; they appear to check that the content-encoding isn't set oddly, and verify the response isn't null, both of which my code would presumably throw an Exception on anyway. They don't appear to do anything like validate the response came from the intended server, which would be unlikely anyway in my particular scenario since it's an HTTPS request. Am I missing out on somet开发者_开发问答hing by doing things this way? I'm certainly gaining a lot in terms of performance!


What you're doing looks totally fine. Your call to PrepareAuthorizedRequest signs the outgoing HTTP request and you're free to send it out asynchronously or otherwise after that without any further thought to OAuth-specific concerns.

I'm not sure what code you're referring to when you mention "verify the response isn't null", etc. But in the testing I've done, there are times when Response (both in the exception and non-exception case) can be null. I don't remember exactly when though.

0

精彩评论

暂无评论...
验证码 换一张
取 消