http请求,获取数据的问题

roro0811 2010-08-26 08:21:27
情况是这样的:
我需要向公司的服务器发送一个http请求,里面还带有一些参数,例如这样:
http://192.168.1.205/index.php?do=getdata&type=event
服务器解析这些参数后,会回一些数据给我
问题是:我怎么才能收到来自服务器的数据?

wininet那套函数可以吗?比如HttpSendRequest

谢谢大家了,我所有的分都在这了!
...全文
133 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyfree 2010-08-26
  • 打赏
  • 举报
回复
下面是msdn上的一个例子,当然有很多不同的写法。
个人最推荐你用libcurl,很不错。我用了以后就不再用wininet了

int WINAPI Dumper(HWND hX, int intCtrlID, HINTERNET hResource)
{
LPTSTR lpszData; // buffer for the data
DWORD dwSize; // size of the data available
DWORD dwDownloaded; // size of the downloaded data
DWORD dwSizeSum=0; // size of the data in the textbox
LPTSTR lpszHolding; // buffer to merge the textbox data and buffer

// Set the cursor to an hourglass.
SetCursor(LoadCursor(NULL,IDC_WAIT));

// This loop handles reading the data.
do
{
// The call to InternetQueryDataAvailable determines the
// amount of data available to download.
if (!InternetQueryDataAvailable(hResource,&dwSize,0,0))
{
printf("InternetQueryDataAvailable failed (%d)\n", GetLastError());
SetCursor(LoadCursor(NULL,IDC_ARROW));
return FALSE;
}
else
{
// Allocate a buffer of the size returned by
// InternetQueryDataAvailable.
lpszData = new TCHAR[dwSize+1];

// Read the data from the HINTERNET handle.
if(!InternetReadFile(hResource,
(LPVOID)lpszData,
dwSize,
&dwDownloaded))
{
printf("InternetReadFile failed (%d)\n", GetLastError());
delete[] lpszData;
break;
}
else
{
// Add a null terminator to the end of the data buffer
lpszData[dwDownloaded]='\0';

// Allocate the holding buffer.
lpszHolding = new TCHAR[dwSizeSum + dwDownloaded + 1];

// Check if there has been any data written
// to the textbox.
if (dwSizeSum != 0)
{
// Retrieve the data stored in the textbox if any
GetDlgItemText(hX,intCtrlID,
(LPTSTR)lpszHolding,
dwSizeSum);

// Add a null terminator at the end of the
// textbox data.
lpszHolding[dwSizeSum]='\0';
}
else
{
// Make the holding buffer an empty string.
lpszHolding[0]='\0';
}

size_t cchDest = dwSizeSum + dwDownloaded + dwDownloaded + 1;
LPTSTR* ppszDestEnd = 0;
size_t* pcchRemaining = 0;

// Add the new data to the holding buffer
HRESULT hr = StringCchCatEx(lpszHolding,
cchDest,
lpszData,
ppszDestEnd,
pcchRemaining,
STRSAFE_NO_TRUNCATION);

if(SUCCEEDED(hr))
{
// Write the holding buffer to the textbox.
SetDlgItemText(hX,intCtrlID,(LPTSTR)lpszHolding);

// Delete the two buffers.
delete[] lpszHolding;
delete[] lpszData;

// Add the size of the downloaded data to the
// textbox data size.
dwSizeSum = dwSizeSum + dwDownloaded + 1;

// Check the size of the remaining data.
// If it is zero, break.
if (dwDownloaded == 0)
break;
else
{
// TODO: Insert error handling code here.
}
}
}
}
}
while(TRUE);

// Close the HINTERNET handle.
InternetCloseHandle(hResource);

// Set the cursor back to an arrow.
SetCursor(LoadCursor(NULL,IDC_ARROW));

return TRUE;
}
skyfree 2010-08-26
  • 打赏
  • 举报
回复
没错,用wininet,msdn上就有例子,实现起来不难。不要用socket!
xengine-qyt 2010-08-26
  • 打赏
  • 举报
回复
socket 很简单,最进也在做这个项目

socket直接 recv 就可以获取 建议用socket
PL23K 2010-08-26
  • 打赏
  • 举报
回复
使用 CInternetSession 我刚提了个类似的问题
#include <afxinet.h> 文件头CInternetSession
CInternetSession session;
CHttpFile *file = NULL;
CString strURL = "http://www.02314.com";//注意URL里http前不能有空格
CString strHtml = ""; //存放网页数据
try{
file = (CHttpFile*)session.OpenURL(strURL);
}catch(CInternetException * m_pException){
file = NULL;
m_pException->m_dwError;
m_pException->Delete();
session.Close();
MessageBox("CInternetException");
}
CString strLine;
if(file != NULL){
while(file->ReadString(strLine) != NULL){
strHtml += strLine;
}
}else{
MessageBox("fail");
}
session.Close();
file->Close();
delete file;
file = NULL;
参考 :http://www.hhuj4.com/a/xuexiziyuan/2010/0826/178.html

18,356

社区成员

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

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