wininet异步发送http请求,在模拟器上面好使,但是真机测试不通

sht2130 2011-01-21 10:40:31
如题,想往服务器上面发送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;
}
...全文
193 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sht2130 2011-01-24
  • 打赏
  • 举报
回复
网络通,问题已经解决了,在连接类中需要添加窗口的句柄,非常感谢楼上的回复:修改代码如下:
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.hWnd = m_hwnd;

sConInfo.bExclusive = FALSE;
sConInfo.bDisabled = FALSE;

sConInfo.guidDestNet = guidNet;//此 GUID 对应接入点

ConnMgrEstablishConnectionSync(&sConInfo, &hConnection, 15000, &dwStatus);
mzoy 2011-01-24
  • 打赏
  • 举报
回复
设备上的网络通?
sht2130 2011-01-21
  • 打赏
  • 举报
回复
顶起来
sht2130 2011-01-21
  • 打赏
  • 举报
回复
自己顶

7,655

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧