gprs wininet
通过gprs拨号连接无线网络,HttpSendRequestEx总返回 12002错误,系统是wince5.0 无线模块是华为em200。
SendDataToServer(CString strWeb, //服务端页面地址。如:www.safewe.com/job1.htm
CString strSendBuffer, //发送数据
LPBYTE lpRecvBuffer, //返回数据保存地址
DWORD dwRecvDataLen, //返回数据长度
CString &strRetHeadInfo, //返回数据头
CString strOpenRequestType, //数据请求类型:这里为POST
DWORD dwPort, //端口
DWORD dwServiceType //服务类型
)
{
CString strServerName = strWeb;
CString strObject = TEXT("");
CString printf = L"";
HINTERNET hOpen;
HINTERNET hConnect;
HINTERNET hRequest;
BOOL bResult = FALSE;
BOOL bRet = FALSE;
DWORD dwSize;
TCHAR* lpHeaders = NULL;
TCHAR szHttpStatus[4] = {0};
int dwHttpStatus = 0;
char *pszData = NULL;
TCHAR head[]=_T("Content-Type: application/x-www-form-urlencoded");
INTERNET_BUFFERS BufferIn = {0};
int nErrorCode;
int n = strWeb.Find(TEXT("/"));
if (n != -1)
{
strServerName = strWeb.Left(n);
strObject = strWeb.Mid(n+1);
}
hOpen = InternetOpen(TEXT("Safewe"), INTERNET_OPEN_TYPE_DIRECT, NULL, 0, 0);
if (!hOpen)
return FALSE;
if (!(hConnect = InternetConnect(hOpen, strServerName, dwPort, NULL, NULL, dwServiceType, 0, 0)))
{
InternetCloseHandle(hOpen);
return FALSE;
}
LPTSTR AcceptTypes[2] = {TEXT("*/*"), NULL};
DWORD dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;
if (!(hRequest = HttpOpenRequest (hConnect, strOpenRequestType, strObject, HTTP_VERSION, NULL,
(LPCTSTR*)AcceptTypes, dwFlags, 0)))
{
InternetCloseHandle(hConnect);
InternetCloseHandle(hOpen);
return FALSE;
}
DWORD dwHttpTimeout = 100000;
if(!InternetSetOption(hRequest,INTERNET_OPTION_CONNECT_TIMEOUT,&dwHttpTimeout,sizeof(DWORD)))
goto END;
if(!InternetSetOption(hRequest,INTERNET_OPTION_SEND_TIMEOUT,&dwHttpTimeout,sizeof(DWORD)))
goto END;
if(!InternetSetOption(hRequest,INTERNET_OPTION_RECEIVE_TIMEOUT,&dwHttpTimeout,sizeof(DWORD)))
goto END;
//重点要注意的地方, HttpSendRequest 的第三个参数必须为多字节编码格式,否则服务器端会出错
pszData = new char[strSendBuffer.GetLength()*2+2];
memset(pszData,0,strSendBuffer.GetLength()*2+2);
WideCharToMultiByte( CP_ACP, 0, strSendBuffer.GetBuffer(0),
-1, pszData, strSendBuffer.GetLength()*2, NULL, NULL);
memset((void *)&BufferIn,0,sizeof(INTERNET_BUFFERS));
BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);
BufferIn.lpvBuffer = pszData;
BufferIn.dwBufferTotal=strlen(pszData);
BufferIn.lpcszHeader = head;
BufferIn.dwHeadersTotal= sizeof(head);
bResult = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0, 0);
if (bResult == FALSE)
{
DWORD errCode = GetLastError();
CString aa;
aa.Format(L"请求失败:%d",errCode);
AfxMessageBox(aa);
}
bResult = InternetWriteFile(hRequest, pszData, strlen(pszData), &dwSize);
bResult = HttpEndRequest(hRequest, NULL, 0, 0);
HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize, NULL);
lpHeaders = new TCHAR [dwSize+1];
ZeroMemory(lpHeaders, sizeof(lpHeaders)/sizeof(TCHAR));
if (!HttpQueryInfo (hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID)lpHeaders, &dwSize, NULL))
{
strRetHeadInfo = lpHeaders;
goto END2;
}
strRetHeadInfo = lpHeaders;
memcpy(szHttpStatus,lpHeaders+wcslen(_T("HTTP/1.1 ")),3*sizeof(TCHAR));
dwHttpStatus =_wtoi(szHttpStatus);
if ((dwHttpStatus<200) || (dwHttpStatus>300))
goto END2;
for (;;) {
dwSize = BUF_SIZE;
if (InternetReadFile(hRequest, (LPVOID)(lpRecvBuffer), dwRecvDataLen, &dwSize) == FALSE) {
int aa;
aa = GetLastError();
break;
}
if (dwSize <= 0) {
break;
}
unsigned short * pszWideChar = new unsigned short[BUF_SIZE + 1];
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)lpRecvBuffer, BUF_SIZE,pszWideChar, BUF_SIZE);
printf += pszWideChar;
//Output value = value1 + value2
}
bRet = TRUE;
END2:
if (lpHeaders)
{
delete lpHeaders;
lpHeaders= NULL;
}
END:
if (pszData)
{
delete pszData;
pszData= NULL;
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hOpen);
return bRet;
}