我执行http请求,当它们失败时,我会重复请求。我通过拔掉网线连接到我的电脑并重新插入来模拟它。大部分时间我的代码都有效,但有时候
co_await HttpClient :: PutAsync()never" return"或完成。
我的代码看起来像这样:
task<Aws::Result> Aws::S3Request::tryExecute()
{
// content
IHttpContent^ httpContent;
httpContent = ref new HttpBufferContent(mBufferContent);
httpContent->Headers->ContentType = ref new Headers::HttpMediaTypeHeaderValue(ref new String(mContentType.c_str()));
std::wstring signature = getSignature();
// client
auto filter = ref new ::Filters::HttpBaseProtocolFilter;
filter->AllowUI = false;
filter->CacheControl->ReadBehavior = Filters::HttpCacheReadBehavior::NoCache;
filter->CacheControl->WriteBehavior = Filters::HttpCacheWriteBehavior::NoCache;
HttpClient^ httpClient = ref new HttpClient(filter);
httpClient->DefaultRequestHeaders->Authorization = ref new Headers::HttpCredentialsHeaderValue(L"AWS", ref new String((mUser.mAccessKey + L":" + signature).c_str()));
httpClient->DefaultRequestHeaders->Append(L"Date", ref new String(mDate.c_str()));
httpClient->DefaultRequestHeaders->Append(L"x-amz-acl", L"public-read");
httpClient->DefaultRequestHeaders->Append(L"x-amz-security-token", ref new String(mUser.mSessionToken.c_str()));
// http req
Uri^ uri = ref new Uri(ref new String(mUri.c_str()));
HttpResponseMessage^ response;
IAsyncOperationWithProgress<HttpResponseMessage^, HttpProgress>^ progress;
try
{
progress = httpClient->PutAsync(uri, httpContent);
progress->Progress = ref new AsyncOperationProgressHandler<HttpResponseMessage^, HttpProgress>(
[this](IAsyncOperationWithProgress<HttpResponseMessage^, HttpProgress>^ pretask, HttpProgress progressInfo)
{
/*double got = progressInfo.BytesReceived + progressInfo.BytesSent;
double total = 0;
if (progressInfo.TotalBytesToReceive)
total += progressInfo.TotalBytesToReceive->Value;
if (progressInfo.TotalBytesToSend)
total += progressInfo.TotalBytesToSend->Value;
float progress = (total >= 1) ? float(got / total) : 0.0f;
debugOut("--- part progress = ", progress);
if (mProgress)
*mProgress = progress;*/
}
);
response = co_await progress;
progress = nullptr;
if (!response->IsSuccessStatusCode)
debugOut("--- http fail error");
}
catch (Platform::COMException^)
{
debugOut("--- http fail nointernet");
progress = nullptr;
mResult = NoInternet;
mAnswer.clear();
co_return mResult;
}
// answer
mAnswer = response->Content->ToString()->Data();
debugOut(L"--- Http answer=", mAnswer);
mResult = response->IsSuccessStatusCode ? Success : Error;
co_return mResult;
}
如果失败,则再次调用tryExecute()。
顺便说一句,IAsyncOperationWithProgress只给我0%或100%而不是inbetweens。我在每个请求中上传了5 MB的大小。
答案 0 :(得分:0)
这有效,但不是一个好的解决方案:
//response = co_await progress;
for (int timeout = 5*60*100; true; timeout--)
{
if (progress->Status != AsyncStatus::Started)
break;
co_await winrt::resume_after(10ms);
if (timeout == 0)
{
debugOut("--- http fail timeout");
mResult = NoInternet;
mAnswer.clear();
co_return mResult;
}
}
response = progress->GetResults();