我现在需要连接一个HTTPS服务器https://221.176.1.140:443/do_login.php,直接把账号写到POST到/do_login.php。HttpSendRequest的时候,总是不成功,返回值是 “Cannot perform http request code: 12057”bool CSslConnection::ConnectToHttpsServer(string &strVerb)
{
try {
m_hInternet = InternetOpen(m_strAgentName.c_str(), INTERNET_OPEN_TYPE_PRECONFIG ,
NULL, NULL, 0);
if (!m_hInternet) {
m_strLastError = "Cannot open internet";
m_lastErrorCode = GetLastError();
return false;
}
m_hSession = InternetConnect(m_hInternet,
m_strServerName.c_str(),
m_wPort,
m_strUserName.c_str(),
m_strPassword.c_str(),
INTERNET_SERVICE_HTTP,
0,
0);
if (!m_hSession) {
m_strLastError = "Cannot connect to internet";
m_lastErrorCode = GetLastError();
ClearHandles();
return false;
}
m_hRequest = HttpOpenRequest(m_hSession,
strVerb.c_str(),
m_strObjectName.c_str(),
NULL,
"",
NULL,
m_secureFlags,
m_ReqID);
if (!m_hRequest) {
m_strLastError = "Cannot perform http request";
m_lastErrorCode = GetLastError();
ClearHandles();
return false;
}
m_ReqID++;
}
catch(...) {
m_strLastError = "Memory Exception occured";
m_lastErrorCode = GetLastError();
return false;
}
return true;
}bool CSslConnection::SendHttpsRequest()
{
try {
for (int tries = 0; tries < 20; ++tries) {
// int result = HttpSendRequest(m_hRequest, NULL, 0, NULL, 0);
int result = HttpSendRequest(m_hRequest, NULL, -1L, NULL, 0); //2011-09-28 -1L
if (result)
return true;
int lastErr = GetLastError();
if (lastErr == ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED) {
if (!SetClientCert()) {
m_strLastError = "Cannot perform http request, client authentication needed but couldnt detect required client certificate";
m_lastErrorCode = GetLastError();
return false;
}
}
else if (lastErr == ERROR_INTERNET_INVALID_CA) {
m_strLastError = "Cannot perform http request, client authentication needed, invalid client certificate is used";
m_lastErrorCode = GetLastError();
return false;
}
else {
m_strLastError = "Cannot perform http request";
m_lastErrorCode = GetLastError();
return false;
}
}
}
catch(...) {
m_strLastError = "Memory Exception occured";
m_lastErrorCode = GetLastError();
return false;
}
return false;
}
{
try {
m_hInternet = InternetOpen(m_strAgentName.c_str(), INTERNET_OPEN_TYPE_PRECONFIG ,
NULL, NULL, 0);
if (!m_hInternet) {
m_strLastError = "Cannot open internet";
m_lastErrorCode = GetLastError();
return false;
}
m_hSession = InternetConnect(m_hInternet,
m_strServerName.c_str(),
m_wPort,
m_strUserName.c_str(),
m_strPassword.c_str(),
INTERNET_SERVICE_HTTP,
0,
0);
if (!m_hSession) {
m_strLastError = "Cannot connect to internet";
m_lastErrorCode = GetLastError();
ClearHandles();
return false;
}
m_hRequest = HttpOpenRequest(m_hSession,
strVerb.c_str(),
m_strObjectName.c_str(),
NULL,
"",
NULL,
m_secureFlags,
m_ReqID);
if (!m_hRequest) {
m_strLastError = "Cannot perform http request";
m_lastErrorCode = GetLastError();
ClearHandles();
return false;
}
m_ReqID++;
}
catch(...) {
m_strLastError = "Memory Exception occured";
m_lastErrorCode = GetLastError();
return false;
}
return true;
}bool CSslConnection::SendHttpsRequest()
{
try {
for (int tries = 0; tries < 20; ++tries) {
// int result = HttpSendRequest(m_hRequest, NULL, 0, NULL, 0);
int result = HttpSendRequest(m_hRequest, NULL, -1L, NULL, 0); //2011-09-28 -1L
if (result)
return true;
int lastErr = GetLastError();
if (lastErr == ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED) {
if (!SetClientCert()) {
m_strLastError = "Cannot perform http request, client authentication needed but couldnt detect required client certificate";
m_lastErrorCode = GetLastError();
return false;
}
}
else if (lastErr == ERROR_INTERNET_INVALID_CA) {
m_strLastError = "Cannot perform http request, client authentication needed, invalid client certificate is used";
m_lastErrorCode = GetLastError();
return false;
}
else {
m_strLastError = "Cannot perform http request";
m_lastErrorCode = GetLastError();
return false;
}
}
}
catch(...) {
m_strLastError = "Memory Exception occured";
m_lastErrorCode = GetLastError();
return false;
}
return false;
}
谢谢你!可是这个我看不懂, 我是个c++初学者调用HttpSendRequest 返回的error code: 12057是什么,msdn的里面没讲清楚