我有一臺服務器,如果我發送它「https://MyDomain/Dir1/Dir2/login.html?u = 1234 & t = 5678」將用xml文檔進行響應。有誰知道我在wininet中如何做到這一點?在C++中使用wininet
這是我迄今所做的:
std::string GetLoginData(void)
{
HINTERNET hInternet, hConnection, hRequest;
std::string retVal;
hInternet = InternetOpen(L"My Application", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if(NULL != hInternet)
{
hConnection = InternetConnect(hInternet, L"MyDomain", INTERNET_DEFAULT_HTTPS_PORT, L"", L"", INTERNET_SERVICE_HTTP, 0, NULL);
if(NULL != hConnection)
{
const wchar_t* lplpszAcceptTypes[] = {L"text/xml", L"application/xml", L"application/xhtml+xml", NULL};
hRequest = HttpOpenRequest(hConnection, L"GET", L"Dir1/Dir2/Login.html", NULL, NULL, lplpszAcceptTypes,
INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS | INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_NO_AUTH | INTERNET_FLAG_NO_UI | INTERNET_FLAG_PRAGMA_NOCACHE, 0);
if(NULL != hRequest)
{
const char Params[] = {"u=1234&t=5678"};
BOOL rc = HttpSendRequest(hRequest, NULL, 0, (void*)Params, strlen(Params));
if(rc) // here rc = 0 and GetLastError() returns 12152
{
DWORD availDataLen;
char Buff[4096];
DWORD readCount = ERROR_INTERNET_CONNECTION_RESET;
InternetQueryDataAvailable(hInternet, &availDataLen, 0, 0);
while(0 < availDataLen)
{
InternetReadFile(hInternet, Buff, min(sizeof(Buff), availDataLen), &readCount);
availDataLen -= readCount;
retVal = retVal + Buff;
}
}
InternetCloseHandle(hRequest);
}
InternetCloseHandle(hConnection);
}
InternetCloseHandle(hInternet);
}
return retVal;
}
但你可以在代碼中看到HttpSendRequest()
失敗並GetLastError()
返回12152
預先感謝您 山姆
謝謝你,實際上是這個問題。 – Sam 2011-04-11 16:53:23