private static String GetConsumerResponseBody(IConsumerRequest request) { var body = String.Empty; using (var response = request.ToWebResponse()) { using (var stream = response.GetResponseStream()) { if (stream != null) { var reader = new StreamReader(stream, Encoding.UTF8); body = reader.ReadToEnd(); } } } return(body); }
public bool doDisconnect() { OAuthSession oas = getOAuthSession(); oas.ConsumerContext.UseHeaderForOAuthParameters = true; oas.AccessToken = getAccessToken(); IConsumerRequest icr = oas.Request(); icr = icr.Get(); icr = icr.ForUrl(QB_DISCONNECT); icr = icr.SignWithToken(); var ret = icr.ToWebResponse(); if (ret.StatusCode.ToInt() == 200) { return(true); } else { return(false); } }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <returns></returns> public static string ReadBody(this IConsumerRequest request) { HttpWebResponse response = request.ToWebResponse(); return(response.ReadToEnd()); }
/// <summary> /// Calls the Xero API. /// </summary> /// <param name="consumerRequest">The consumer request.</param> /// <param name="accessTokenRepository">The access token repository.</param> /// <returns></returns> private Response CallXeroApiInternal(IConsumerRequest consumerRequest, ITokenRepository <AccessToken> accessTokenRepository) { AccessToken accessToken = accessTokenRepository.GetToken(""); if (accessToken == null) { return(new Response { Status = "NotConnected" }); } if (accessToken.HasExpired()) { accessToken = RenewAccessToken(accessTokenRepository); } if (accessToken.HasExpired()) { return(new Response { Status = "AccessTokenExpired" }); } // At this point, we should have a valid a consumerRequest.SignWithToken(accessToken); HttpWebResponse webResponse; try { webResponse = consumerRequest.ToWebResponse(); } catch (WebException ex) { if (ex.Response is HttpWebResponse) { HttpWebResponse httpWebResponse = (HttpWebResponse)ex.Response; if (httpWebResponse.StatusCode == HttpStatusCode.NotFound) { return(new Response { Status = "NotFound" }); } } return(new Response { Status = "Web Exception: " + ex.Message }); } catch (OAuthException ex) { return(new Response { Status = "OAuth Exception: " + ex.Report }); } catch (Exception ex) { return(new Response { Status = "Exception: " + ex.Message }); } return(ModelSerializer.DeSerializer <Response>(webResponse.GetResponseStream().ReadToEnd())); }