wininet异步发送http请求,在模拟器上面好使,但是真机测试不通
如题,想往服务器上面发送http请求,模拟器发送和接受都好使,但是真机测试的时候InternetWriteFile报12019错误,请求发送不到服务器上面,尝试了不少方法都没有效果。
代码如下:
DWORD dwStatus = 0;
HANDLE hConnection = NULL;
GUID guidNet = {0x436ef144, 0xb4fb, 0x4863, 0xa0, 0x41, 0x8f, 0x90, 0x5a, 0x62, 0xc5, 0x72};
GUID guidWAP = {0x7022e968, 0x5a97, 0x4051, 0xbc, 0x1c, 0xc5, 0x78, 0xe2, 0xfb, 0xa5, 0xd9};//WAP
CONNMGR_CONNECTIONINFO sConInfo;
memset(&sConInfo, 0 ,sizeof(CONNMGR_CONNECTIONINFO));
sConInfo.cbSize = sizeof(CONNMGR_CONNECTIONINFO);
sConInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
sConInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
sConInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP;
/*|CONNMGR_FLAG_PROXY_WAP
|CONNMGR_FLAG_PROXY_SOCKS4
|CONNMGR_FLAG_PROXY_SOCKS5; */
sConInfo.bExclusive = FALSE;
sConInfo.bDisabled = FALSE;
sConInfo.guidDestNet = guidNet;//此 GUID 对应接入点
ConnMgrEstablishConnectionSync(&sConInfo, &hConnection, 15000, &dwStatus);
int len = WideCharToMultiByte(CP_ACP, 0, requestBody, -1, NULL, 0, NULL, NULL);
char *reqBodyGBK=new char[len];
memset(reqBodyGBK, 0, len);
WideCharToMultiByte (CP_ACP, 0, requestBody, -1, reqBodyGBK, len, NULL,NULL);
// Server Configura
CString server = _T("211.137.XX.XX");
int nport = 9200;
CString webpath = _T("Test/TestServlet");
// http headerInfo
CString post = _T("POST");
CString activeID = _T("swsn2");
CString userAgent = _T("User-Agent:") + activeID;
CString serviceType = _T("1111/1.0");
CString xServiceType = _T("x-service-type:") + serviceType;
CString protocolVersion = _T("1.0");
CString xProtocolVersion = _T("x-protocol-version:") + protocolVersion;
int iret = 0;
int ireslen = 0;
DWORD dwNumKSent;
DWORD dwNumKToSend;
BOOL bAllDone = FALSE;
HINTERNET hInstance = NULL;
REQUEST_CONTEXT httpreq;
const int HTTP_REQUEST_TIMEOUT_WINDOWS = 8000;
httpreq.dwNumBytesComplete = 0;
dwNumKToSend = strlen(reqBodyGBK);
httpreq.hConnect = NULL;
httpreq.hRequest = NULL;
httpreq.hConnectedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
httpreq.hRequestCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
hInstance = InternetOpen(_T("async_http"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
//hInstance = InternetOpen(_T("async_http"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hInstance == NULL)
{
//printf("InternetOpen failed, error %d\n", GetLastError());
iret = -1;
goto EXIT_SENDPOST;
}
if (InternetSetStatusCallback(hInstance, (INTERNET_STATUS_CALLBACK)&Callback) == INTERNET_INVALID_STATUS_CALLBACK)
{
//printf("InternetSetStatusCallback failed, error %d\n", GetLastError());
iret = -1;
goto EXIT_SENDPOST;
}
httpreq.ntype = 1; //connect
httpreq.hConnect = InternetConnect(hInstance, server, nport, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD)&httpreq);
if (httpreq.hConnect == NULL)
{
if (GetLastError() != ERROR_IO_PENDING)
{
//printf("InternetConnect failed, error %d\n", GetLastError());
iret = -1;
goto EXIT_SENDPOST;
}
WaitForSingleObject(httpreq.hConnectedEvent, HTTP_REQUEST_TIMEOUT_WINDOWS);
}
httpreq.ntype = 2; //Request
httpreq.hRequest = HttpOpenRequest(httpreq.hConnect, post, webpath, NULL, NULL,NULL,
INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE, (DWORD)&httpreq);//2);
if (httpreq.hRequest == NULL)
{
if (GetLastError() != ERROR_IO_PENDING)
{
//printf("HttpOpenRequest failed, error %d\n", GetLastError());
iret = -1;
goto EXIT_SENDPOST;
}
WaitForSingleObject(httpreq.hRequestCompleteEvent, HTTP_REQUEST_TIMEOUT_WINDOWS);
}
// add HeaderInfo
HttpAddRequestHeaders(httpreq.hRequest,userAgent,-1,HTTP_ADDREQ_FLAG_ADD);
HttpAddRequestHeaders(httpreq.hRequest,xServiceType,-1,HTTP_ADDREQ_FLAG_ADD);
INTERNET_BUFFERS IntBuff;
FillMemory(&IntBuff, sizeof(IntBuff), 0);
IntBuff.dwStructSize= sizeof(IntBuff);
IntBuff.dwBufferTotal = dwNumKToSend;
//IntBuff.lpcszHeader = "Content-Type: text/text\r\n";
//IntBuff.lpcszHeader = "Content-Type: text/text";
//IntBuff.dwHeadersLength = lstrlen(IntBuff.lpcszHeader);
httpreq.ntype = 2; //Request
if (!HttpSendRequestEx(httpreq.hRequest, &IntBuff, NULL, 0, (DWORD)&httpreq))
{
DWORD message = GetLastError();
if (GetLastError() != ERROR_IO_PENDING)
{
printf("HttpSendRequestEx failed, error %d\n", GetLastError());
iret = -1;
goto EXIT_SENDPOST;
}
//printf("HttpSendRequestEx called successfully\n");
WaitForSingleObject(httpreq.hRequestCompleteEvent, HTTP_REQUEST_TIMEOUT_WINDOWS);
}
for (dwNumKSent = 0; dwNumKSent < dwNumKToSend; dwNumKSent++)
{
DWORD dwBytesWritten;
if(!InternetWriteFile(httpreq.hRequest, reqBodyGBK, dwNumKToSend, &dwBytesWritten))
{
DWORD message = GetLastError();
if (GetLastError() != ERROR_IO_PENDING)
{
printf("InternetWriteFile failed, error %d\n", GetLastError());
iret = -1;
goto EXIT_SENDPOST;
}
else
{
//printf("InternetWriteFile completing asynchronously\n");
WaitForSingleObject(httpreq.hRequestCompleteEvent, HTTP_REQUEST_TIMEOUT_WINDOWS);
}
}
}
//printf("Calling HttpEndRequest\n");
BOOL flag = HttpEndRequest(httpreq.hRequest, NULL, HSR_INITIATE, 2);
if (!flag)
{
if (GetLastError() == ERROR_IO_PENDING)
{
//printf("HttpEndRequest called\n");
WaitForSingleObject(httpreq.hRequestCompleteEvent, HTTP_REQUEST_TIMEOUT_WINDOWS);
}
else
{
printf("HttpEndRequest failed, error %d\n", GetLastError());
iret = -1;
goto EXIT_SENDPOST;
}
}
// getResponseHeader
TCHAR pszHeader[4*1024]={0};
DWORD bufferLength;
HttpQueryInfo(httpreq.hRequest,HTTP_QUERY_RAW_HEADERS_CRLF ,NULL,&bufferLength,NULL) ;
if (HttpQueryInfo(httpreq.hRequest,HTTP_QUERY_RAW_HEADERS_CRLF ,pszHeader,&bufferLength,NULL))
{
CString respHeader;
respHeader.Format(_T("%s"),pszHeader);
int idx = respHeader.Find(_T("x-status-code"));
CString status = respHeader.Mid(idx+15,3);
int respStatus = _ttoi(status);
if (respStatus!=200)
{
iret = respStatus;
goto EXIT_SENDPOST;
} else
{
iret=200;
}
} else {
printf("HttpQueryInfo failed, error %d\n", GetLastError());
iret = -1;
goto EXIT_SENDPOST;
}