【发布时间】:2015-12-16 20:52:41
【问题描述】:
最初我想发送没有客户端证书的第一个请求,如果错误是“身份验证错误”,那么我想重新发送附有证书的请求。所以代码首先是
HttpStringContent ehttpContent = new HttpStringContent(content);
HttpRequestMessage req2 = new HttpRequestMessage(method, resourceUri);
req2.Headers.Add("SOAPAction", soapAction);
req2.Content = ehttpContent;
req2.Content.Headers.ContentType = HttpMediaTypeHeaderValue.Parse("text/xml; charset=utf-8");
bool retry= await FirstLoginAttempt(...);
if( retry) {
newHttpClient = new HttpClient(baseFilter2);
try
{
cts.CancelAfter(Constants.HttpTimeOut);
System.Diagnostics.Debug.WriteLine(ehttpContent);
eresponse = await newHttpClient.SendRequestAsync(req2).AsTask(cts.Token);
if (cts != null)
{
cts.Dispose();
cts = null;
} catch { ... }
}
前面的代码调用 FirstLoginAttempt 可能会因证书错误而失败。它返回 true 或 false,然后前面的代码尝试发送带有证书的新 httpClient。
public static async Task<bool> FirstLoginAttempt() {
httpClient = new HttpClient();
try
{
cts.CancelAfter(Constants.HttpTimeOut);
var operation = httpClient.SendRequestAsync(ereq).AsTask(cts.Token);
eresponse = await operation;
if (cts != null) { cts.Dispose(); cts = null; }
}
catch (TaskCanceledException e)
{
if (cts != null) { cts.Dispose(); cts = null; }
throw e;
}
catch (Exception exx)
{ ... }
}
当第一次登录尝试返回 true 以便再次尝试时,第二个 httpclient(具有相同的内容)失败并且异常说我不能两次发送相同的请求。那可能意味着之前的操作没有完成?我该如何解决我的问题?我能否以某种方式确定第一个请求生命周期已结束以便重试?
我的环境是Windows Phone 8.1
更新 #1。 使用 lambda 后
StackTrace = " 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n ... Message = "HRESULT 异常:0x80072EFD"(不成功时抛出)
在堆栈跟踪之上 ... ...
Func<HttpRequestMessage> httpRequestCreator = () =>
{
HttpStringContent ehttpContent = new HttpStringContent(content);
var req = new HttpRequestMessage(method, resourceUri)
{
Content = ehttpContent
};
req.Headers.Add("SOAPAction", soapAction);
req.Content.Headers.ContentType = HttpMediaTypeHeaderValue.Parse("text/xml; charset=utf-8");
return req;
};
if (cts != null) { cts.Dispose(); cts = null; }
cts = new System.Threading.CancellationTokenSource();
newHttpClient = new HttpClient();
try
{
cts.CancelAfter(Constants.HttpTimeOut);
eresponse = await newHttpClient.SendRequestAsync(httpRequestCreator()).AsTask(cts.Token);
if (cts != null) { cts.Dispose(); cts = null; }
}
catch (TaskCanceledException e
{
if (cts != null) { cts.Dispose(); cts = null; }
throw e;
}
catch (Exception exx)
{
exception2 = exx;
...
...
}
【问题讨论】:
标签: c# .net windows-phone-8.1 dotnet-httpclient asynchttpclient