/// <summary> /// Downloads a report to stream. /// </summary> /// <param name="downloadUrl">The download url.</param> /// <param name="postBody">The POST body.</param> private ReportResponse DownloadReport(string downloadUrl, string postBody) { AdWordsErrorHandler errorHandler = new AdWordsErrorHandler(this.User as AdWordsUser); while (true) { WebResponse response = null; HttpWebRequest request = BuildRequest(downloadUrl, postBody); LogEntry logEntry = new LogEntry(User.Config, new DefaultDateTimeProvider()); logEntry.LogRequest(request, postBody, HEADERS_TO_MASK); try { response = request.GetResponse(); logEntry.LogResponse(response, false, "Response truncated."); logEntry.Flush(); return(new ReportResponse(response)); } catch (WebException e) { string contents = ""; Exception reportsException = null; using (response = e.Response) { try { contents = MediaUtilities.GetStreamContentsAsString( response.GetResponseStream()); } catch { contents = e.Message; } logEntry.LogResponse(response, true, contents); logEntry.Flush(); reportsException = ParseException(e, contents); if (AdWordsErrorHandler.IsOAuthTokenExpiredError(reportsException)) { reportsException = new AdWordsCredentialsExpiredException( request.Headers["Authorization"]); } } if (errorHandler.ShouldRetry(reportsException)) { errorHandler.PrepareForRetry(reportsException); } else { throw reportsException; } } } }
/// <summary> /// Downloads a report to stream. /// </summary> /// <param name="downloadUrl">The download url.</param> /// <param name="returnMoneyInMicros">True if money values are returned /// in micros.</param> /// <param name="postBody">The POST body.</param> /// <param name="outputStream">The stream to which report is downloaded. /// </param> private void DownloadReportToStream(string downloadUrl, bool?returnMoneyInMicros, string postBody, Stream outputStream) { AdWordsErrorHandler errorHandler = new AdWordsErrorHandler(user); while (true) { WebResponse response = null; HttpWebRequest request = BuildRequest(downloadUrl, returnMoneyInMicros, postBody); try { response = request.GetResponse(); MediaUtilities.CopyStream(response.GetResponseStream(), outputStream); return; } catch (WebException ex) { Exception reportsException = null; try { response = ex.Response; if (response != null) { MemoryStream memStream = new MemoryStream(); MediaUtilities.CopyStream(response.GetResponseStream(), memStream); String exceptionBody = Encoding.UTF8.GetString(memStream.ToArray()); reportsException = ParseException(exceptionBody); } if (AdWordsErrorHandler.IsOAuthTokenExpiredError(reportsException)) { reportsException = new AdWordsCredentialsExpiredException( request.Headers["Authorization"]); } } catch (Exception) { reportsException = ex; } if (errorHandler.ShouldRetry(reportsException)) { errorHandler.PrepareForRetry(reportsException); } else { throw reportsException; } } finally { if (response != null) { response.Close(); } } } }
/// <summary> /// Downloads a report to stream. /// </summary> /// <param name="downloadUrl">The download url.</param> /// <param name="postBody">The POST body.</param> private ReportResponse DownloadReport(string downloadUrl, string postBody) { AdWordsErrorHandler errorHandler = new AdWordsErrorHandler(this.User as AdWordsUser); while (true) { WebResponse response = null; HttpWebRequest request = null; LogEntry logEntry = new LogEntry(User.Config, new DefaultDateTimeProvider()); try { request = BuildRequest(downloadUrl, postBody, logEntry); response = request.GetResponse(); logEntry.LogResponse(response, false, "Response truncated."); logEntry.Flush(); return(new ReportResponse(response)); } catch (WebException e) { Exception reportsException = null; string contents = HttpUtilities.GetErrorResponseBody(e); logEntry.LogResponse(e.Response, true, contents); logEntry.Flush(); reportsException = ParseException(e, contents); if (AdWordsErrorHandler.IsOAuthTokenExpiredError(reportsException) && request != null) { reportsException = new AdWordsCredentialsExpiredException( request.Headers["Authorization"]); } if (errorHandler.ShouldRetry(reportsException)) { errorHandler.PrepareForRetry(reportsException); } else { throw reportsException; } } finally { featureUsageRegistry.Clear(); } } }