일부 XML 데이터 (http://freecite.library.brown.edu/welcome/api_instructions)를 가져 오기 위해 API에 POST 요청을해야합니다. 이 curl
와 함께 잘 작동 : wininet을 사용하여 POST 요청 응답을 얻을 수 없습니다.
curl -H "Accept: application/xml" --data "citation=Udvarhelyi, I.S., Gatsonis, C.A., Epstein, A.M., Pashos, C.L., Newhouse, J.P. and McNeil, B.J. Acute Myocardial Infarction in the Medicare population: process of care and clinical outcomes. Journal of the American Medical Association, 1992; 18:2530-2536. " http://freecite.library.brown.edu:80/citations/create
는 그래서는 Win32 SDK를 사용하여 유사한 말았을하려고하고있다. ,
availableBytes
이 결과 0 나옵니다도
InternetQueryDataAvailable
에 대한 호출 후
for
루프에 들어가기 전에
이
void LoadData()
{
wil::unique_hinternet hInternet(InternetOpen(L"Dummy", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0));
wil::unique_hinternet hConnect(InternetConnect(hInternet.get(), L"http://freecite.library.brown.edu", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0));
wil::unique_hinternet hRequest(HttpOpenRequest(hConnect.get(), L"POST", L"/citations/create", NULL, NULL, NULL, NULL, NULL));
wstring data = L"citation=Udvarhelyi, I.S., Gatsonis, C.A., Epstein, A.M., Pashos, C.L., Newhouse, J.P. and McNeil, B.J. Acute Myocardial Infarction in the Medicare population: process of care and clinical outcomes. Journal of the American Medical Association, 1992; 18:2530-2536.";
PCWSTR szHeaders = L"Accept: application/xml";
HttpSendRequest(hRequest.get(), szHeaders, 0, (LPVOID)data.c_str(), static_cast<int>(data.length()));
DWORD availableBytes = 0;
InternetQueryDataAvailable(hRequest.get(), &availableBytes, 0, 0);
PBYTE outputBuffer = (PBYTE)HeapAlloc(GetProcessHeap(), 0, availableBytes);
PBYTE nextBytes = outputBuffer;
DWORD bytesUsed = 0; // number of used bytes.
while (availableBytes)
{
DWORD downloadedBytes;
InternetReadFile(hRequest.get(), nextBytes, availableBytes, &downloadedBytes);
bytesUsed = bytesUsed + downloadedBytes;
InternetQueryDataAvailable(hRequest.get(), &availableBytes, 0, 0);
if (availableBytes > 0)
{
// lazy buffer growth here. Only alloc for what we need. could be optimized if we end up with huge payloads (>10MB).
// otherwise, this is good enough.
outputBuffer = (PBYTE)HeapReAlloc(GetProcessHeap(), 0, outputBuffer, bytesUsed + availableBytes);
nextBytes = outputBuffer + bytesUsed; // careful, pointer might have moved! Update cursor.
}
}
// Convert outputed XML to wide char
int size_needed = MultiByteToWideChar(CP_UTF8, 0, (PCCH)outputBuffer, bytesUsed, NULL, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, (PCCH)outputBuffer, bytesUsed, &wstrTo[0], size_needed);
wstring res = wstrTo;
}
문제는, 나는 마침내 응답으로 빈 문자열을 그만 둘입니다 : 이것은 내 코드입니다 반면 XML 응답을 기대하고있었습니다.
누구나 내가 잘못하고있는 것을 나에게 지적 할 수 있으며 어떻게 고칠 수 있습니까?
당신은 API 호출 중 하나에서 처리 오류를 안하고을 반환해야합니다. 그들 중 누구라도 실패 할 수 있고 당신은 그것을 알지 못할 것입니다. 또한 응답 데이터를 읽기 전에'HttpQueryInfo()'를 사용하여 요청이 실제로 성공했는지 확인하기 위해 서버의 응답 코드를 얻을 수있다. –
동의했다, 나는'GetLastError()'호출을 추가하고 그것이 송신 요청 자체에서 실패하고있는 것을 발견했다. – SexyBeast