WinInetAPI问题,HttpSendRequest总返回12007
WinInetAPI问题,HttpSendRequest总返回12007.
代码如下,哪位大侠帮看看(刚注册也没啥分,先谢谢了)。
HINTERNET hSession = NULL;
WCHAR m_Url[MAX_URL_LEN] = {0};
WCHAR m_GetEmail[MAX_URL_LEN] = {0};
CString getEmail = _T("");
SetCursor(LoadCursor(NULL, IDC_WAIT));
hSession = InternetOpen (L"WapClient", INTERNET_OPEN_TYPE_PRECONFIG, NULL, 0, 0);
if (hSession == NULL)
{
AfxMessageBox(L"调用InternetOpen失败。");
goto Error;
}
//连接服务器
wcscpy(m_Url, L"http://192.168.8.70/mig-mobile-war/receive");//内网wap站点
HINTERNET hConnect = InternetConnect(hSession,
m_Url,
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
0);
if (hConnect == NULL)
{
AfxMessageBox(L"调用InternetConnect失败。");
goto Error;
}
//创建请求
LPCWSTR acceptTypes[2] = {TEXT("*/*"), NULL};
DWORD flags = INTERNET_FLAG_RELOAD |
INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_KEEP_CONNECTION | // This is necessary if authentication is required.
INTERNET_FLAG_NO_COOKIES; // This is used to avoid possible server errors on successive sessions.
/*flags = flags
| INTERNET_FLAG_SECURE
| INTERNET_FLAG_IGNORE_CERT_CN_INVALID
| INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
;*/
HINTERNET hRequest = HttpOpenRequest(hConnect,
L"POST",
L"",
L"HTTP/1.1",
//HTTP_VERSION,
NULL,
acceptTypes,
flags,
0);
if (hRequest == NULL)
{
AfxMessageBox(L"调用HttpOpenRequest失败。");
goto Error;
}
//发送请求
//CString getEmail = _T("");
getEmail.Format(L"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
L"<xml>\r\n"
L"<action>receive</action>\r\n"
L"<cell id=\"13910019097\"></cell>\r\n"
L"<password>%s</password>\r\n"
L"<number>%d</number>\r\n"
L"</xml>\r\n", L"nidaye", 2);
//CString->char *
int nSize = getEmail.GetLength();
char *msg = new char[nSize + 1];
memset(msg, 0, nSize + 1);
wcstombs(msg, getEmail, nSize + 1);
unsigned int contentLength = 0;
//
// Prepares headers
//
WCHAR headers[512];
contentLength = (msg != NULL)?strlen(msg):0;
const void* msgToSend = (const void*)msg;
wsprintf(headers, TEXT("Content-Type: %s\r\nContent-Length: %d"), TEXT("application/vnd.syncml+xml"), contentLength);
int numretries;
for (numretries = 0; numretries < 3; numretries++)
{
if (!HttpSendRequest(hRequest, headers, wcslen(headers), (LPVOID)msgToSend, contentLength))
{
int errorCode = GetLastError();
CString tmp;
tmp.Format(L"%d", errorCode);
AfxMessageBox(tmp); //此处总返回12007
//AfxMessageBox(L"调用HttpSendRequest失败。");
goto Error;
}
}