本文整理汇总了C#中IWebClient.SetAuthInfo方法的典型用法代码示例。如果您正苦于以下问题:C# IWebClient.SetAuthInfo方法的具体用法?C# IWebClient.SetAuthInfo怎么用?C# IWebClient.SetAuthInfo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IWebClient
的用法示例。
在下文中一共展示了IWebClient.SetAuthInfo方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryDownloadAsync
async Task TryDownloadAsync(TransferSpec spec, IWebClient webClient, IAbsoluteFilePath tmpFile) {
try {
tmpFile.RemoveReadonlyWhenExists();
if (!string.IsNullOrWhiteSpace(spec.Uri.UserInfo))
webClient.SetAuthInfo(spec.Uri);
using (webClient.HandleCancellationToken(spec))
await webClient.DownloadFileTaskAsync(spec.Uri, tmpFile.ToString()).ConfigureAwait(false);
VerifyIfNeeded(spec, tmpFile);
_fileOps.Move(tmpFile, spec.LocalFile);
} catch (OperationCanceledException e) {
_fileOps.DeleteIfExists(tmpFile.ToString());
throw CreateTimeoutException(spec, e);
} catch (WebException ex) {
_fileOps.DeleteIfExists(tmpFile.ToString());
var cancelledEx = ex.InnerException as OperationCanceledException;
if (cancelledEx != null)
throw CreateTimeoutException(spec, cancelledEx);
if (ex.Status == WebExceptionStatus.RequestCanceled)
throw CreateTimeoutException(spec, ex);
var response = ex.Response as HttpWebResponse;
if (response == null)
throw GenerateDownloadException(spec, ex);
switch (response.StatusCode) {
case HttpStatusCode.NotFound:
throw new RequestFailedException("Received a 404: NotFound response", ex);
case HttpStatusCode.Forbidden:
throw new RequestFailedException("Received a 403: Forbidden response", ex);
case HttpStatusCode.Unauthorized:
throw new RequestFailedException("Received a 401: Unauthorized response", ex);
}
throw GenerateDownloadException(spec, ex);
}
}
示例2: TryUploadAsync
static async Task TryUploadAsync(TransferSpec spec, IWebClient webClient) {
try {
if (!string.IsNullOrWhiteSpace(spec.Uri.UserInfo))
webClient.SetAuthInfo(spec.Uri);
using (webClient.HandleCancellationToken(spec))
await webClient.UploadFileTaskAsync(spec.Uri, spec.LocalFile.ToString()).ConfigureAwait(false);
} catch (OperationCanceledException e) {
throw CreateTimeoutException(spec, e);
} catch (WebException e) {
var cancelledEx = e.InnerException as OperationCanceledException;
if (cancelledEx != null)
throw CreateTimeoutException(spec, cancelledEx);
if (e.Status == WebExceptionStatus.RequestCanceled)
throw CreateTimeoutException(spec, e);
GenerateUploadException(spec, e);
}
}
示例3: TryDownloadAsync
async Task TryDownloadAsync(TransferSpec spec, IWebClient webClient, IAbsoluteFilePath tmpFile) {
try {
tmpFile.RemoveReadonlyWhenExists();
if (!string.IsNullOrWhiteSpace(spec.Uri.UserInfo))
webClient.SetAuthInfo(spec.Uri);
using (webClient.HandleCancellationToken(spec))
await webClient.DownloadFileTaskAsync(spec.Uri, tmpFile.ToString()).ConfigureAwait(false);
VerifyIfNeeded(spec, tmpFile);
_fileOps.Move(tmpFile, spec.LocalFile);
} catch (OperationCanceledException e) {
_fileOps.DeleteIfExists(tmpFile.ToString());
throw CreateTimeoutException(spec, e);
} catch (WebException e) {
_fileOps.DeleteIfExists(tmpFile.ToString());
var cancelledEx = e.InnerException as OperationCanceledException;
if (cancelledEx != null)
throw CreateTimeoutException(spec, cancelledEx);
if (e.Status == WebExceptionStatus.RequestCanceled)
throw CreateTimeoutException(spec, e);
GenerateDownloadException(spec, e);
}
}