高分求救! winInet API问题:用InternetWriteFile() 为什么POST不上数据?

edoch 2006-06-06 06:04:00

提交给HTTP Server端的始终只有HttpSendRequestEx()中的数据,用InternetWriteFile()提交的POST数据没起作用,请大侠们指教!

......
requestUrl = m_szIndexURL;
static LPSTR accept[2]={"text/*", NULL};

m_hRequest = HttpOpenRequest(m_hConnect,
"POST",
requestUrl,
NULL,
NULL,
(const char**)&accept[0],
INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE,
(LPARAM)this);

if (m_hRequest == NULL)
{
if (GetLastError() != ERROR_IO_PENDING)
{
return FALSE;
}
WaitForSingleObject(m_hConnectedEvent, INFINITE);
}

static TCHAR szHeader[] = "Content-Type: application/x-www-form-urlencoded";
INTERNET_BUFFERS IntBuff;
int nSendDataLength = strlen(szHeader);

FillMemory(&IntBuff, sizeof(IntBuff), 0);
IntBuff.dwStructSize= sizeof(IntBuff);
IntBuff.dwBufferTotal = nSendDataLength+1;
IntBuff.lpcszHeader = szHeader;
IntBuff.dwHeadersLength = lstrlen(IntBuff.lpcszHeader);

if (!HttpSendRequestEx(m_hRequest,
&IntBuff,
NULL,
0,
(LPARAM)this))
{
if (GetLastError() != ERROR_IO_PENDING)
{
return FALSE;
}

WaitForSingleObject(m_hRequestCompleteEvent, INFINITE);
}

DWORD dwBytesWritten;
char szSendData[1024];
memset(szSendData, 0, 1024);
strcpy(szSendData, frmData.GetBuffer(frmData.GetLength()));//POST数据
int nLen = strlen(szSendData);

if(!InternetWriteFile(m_hRequest,
szSendData,
nLen+1,
&dwBytesWritten))
{
if (GetLastError() != ERROR_IO_PENDING)
{
return FALSE;
}
else
{
WaitForSingleObject(m_hRequestCompleteEvent, INFINITE);
}
}

if (!HttpEndRequest(m_hRequest, NULL, HSR_INITIATE, (LPARAM)this))
{
if (GetLastError() == ERROR_IO_PENDING)
{
WaitForSingleObject(m_hRequestCompleteEvent, INFINITE);
}
else
{
return FALSE;
}
}
......
...全文
232 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ppzine 2006-06-07
  • 打赏
  • 举报
回复
下面是调用:
CString strServer;
CString strObject;
INTERNET_PORT nPort;
AfxParseURL("你要post数据的url",dwServiceType,strServer,strObject,nPort);
CString response=SendData(strObject,strServer,"postparm=aaa");
ppzine 2006-06-07
  • 打赏
  • 举报
回复
给你点可用的代码:
CString SendData(LPCTSTR serverfile,LPCTSTR host,LPCTSTR body)
{
CString strHeaders =_T("Content-Type: application/x-www-form-urlencoded");
CInternetSession session;
CHttpConnection* pConnection;
CHttpFile* pFile;
CString tmp,returnstr;

try
{
pConnection=session.GetHttpConnection(host);
pFile =pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,(LPCTSTR)serverfile);
if(!pFile->SendRequest(strHeaders,(LPVOID)(LPCTSTR)body,strlen(body)+1))
{
returnstr="Err";
}
while(pFile->ReadString(tmp))
{
returnstr+=tmp;
}
pFile->Close();
delete pFile;
pConnection->Close();
delete pConnection;
session.Close();
delete session;

}catch(CException *e)
{
e->Delete();
returnstr="Err";
}

return returnstr;
}
aa3000 2006-06-07
  • 打赏
  • 举报
回复
看看 GetLastError() 返回的是什么错误,自己判断一下
lixiaosan 2006-06-07
  • 打赏
  • 举报
回复
http://support.microsoft.com/kb/184352/en-us

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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