在vc中如何用post方法提交表单

我这里有一段程序,用来在一个对话框里显示出一次http request的原始信息,不过使用Inet API做的,希望能有帮助。

// Allocates a buffer of the size returned by InternetQueryDataAvailable lpszData = new char[dwSize+1]; // Reads the data from the HINTERNET handle. if(!InternetReadFile(hRequest,(LPVOID)lpszData,dwSize,&dwDownloaded)) { delete[] lpszData; break; } else { // Adds a null terminator to the end of the data buffer lpszData[dwDownloaded]='\0'; int nLen = m_editContentGot.GetWindowTextLength(); m_editContentGot.SetSel(nLen-1, nLen-1); m_editContentGot.ReplaceSel(lpszData); // Delete the two buffers delete[] lpszData; // Check the size of the remaining data. If it is zero, break. if (dwDownloaded == 0) break; } } } // Close the HINTERNET handle InternetCloseHandle(hRequest); InternetCloseHandle(hSession); InternetCloseHandle(hInternet); // Set the cursor back to an arrow SetCursor(LoadCursor(NULL,IDC_ARROW));


使用MFC示例如下:
首先设置m_strRequest请求字符串 eg."name=aaa&pass=bbb";
m_strServerName 服务器名称或者IP eg."www.yahoo.com"
m_strObjectName 请求文件位置 eg. "pub/aaa.asp"
请求的结果存放在m_strHtml中

func(){ CInternetSession m_InetSession("session"); CHttpConnection* pServer = NULL; CHttpFile* pFile = NULL; try{ INTERNET_PORT nPort; nPort=80; pServer = m_InetSession.GetHttpConnection(m_strServerName, nPort); pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, m_strObjectName); char szHeaders[100]; strcpy(szHeaders,"Accept: text*/*\r\nContent-Type: application/x-www-form-urlencoded"); pFile->AddRequestHeaders(szHeaders); pFile->SendRequestEx(m_strRequest.GetLength()); pFile->WriteString(m_strRequest); //重要-->m_Request 中有"name=aaa&name2=BBB&..." pFile->EndRequest(); DWORD dwRet; pFile->QueryInfoStatusCode(dwRet); CString str; m_Mutex.Lock(); m_strHtml=""; char szBuff[1024]; if (dwRet == HTTP_STATUS_OK){ UINT nRead; while ((nRead = pFile->Read(szBuff,1023))>0) { m_strHtml+=CString(szBuff,nRead); } } m_Mutex.Unlock(); delete pFile; delete pServer; } catch (CInternetException* e){ CString s; s.Format("Internet Exception\r\nm_dwError%u,m_dwContextError%u",e->m_dwError,e->m_dwContext); AfxMessageBox(s); //catch errors from WinInet }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值