InternetReadFile转换为C中的字符*
我在下面的代码中遇到了一些问题。我见过很多使用InternetReadFile保存到文件的示例。但我无法找到一个,或让它为char []工作。我想添加szBuffer来获取holdBuff,然后设置内容等于holdBuff。InternetReadFile转换为C中的字符*
#include <stdio.h>
#include <tchar.h>
#include <string.h>
#include <windows.h>
#include <WinInet.h>
HINTERNET hSession;
void urlToChar(char* url, char** content);
int main()
{
hSession = InternetOpen("Mozilla/4.0 (compatible) Poison", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
char* content;
urlToChar("http://google.com/", &content);
printf("%s",content);
return 0;
}
void urlToChar(char* url, char** content)
{
HINTERNET hConnect = InternetConnect(hSession, _T(""),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = InternetOpenUrl(hSession, url, NULL, 0, 0, 0);
if (hRequest)
{
char holdBuff[] = "";
char szBuff[1025];
memset(szBuff, 0x00, sizeof(szBuff));
DWORD bytesRead;
while (InternetReadFile(hRequest, szBuff, 1024, &bytesRead) == TRUE && bytesRead > 0)
{
// Cat szBuff to holdBuff
memset(szBuff, 0x00, sizeof(szBuff));
}
*content = holdBuff;
// memset(holdBuff, 0x00, sizeof(holdBuff)); <-- Need this?
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
}
变量声明
char xyz[] = "Hello World!";
会告诉编译器把字符串的内容在堆栈上。当然,函数返回时堆栈会消失。
所以你的情况:
char holdBuff[] = "";
...
*content = holdBuff;
这告诉编译器创建一个长度(NULL终止)作为一个局部变量的字符串。仅仅因为您已将content
的值设置为holdBuff
并不意味着holdBuff
指向的内容已存在。
你必须纠正两件事。首先,您必须使用strcpy()
或类似的功能。其次,您必须为holdBuff
分配足够的空间。
例子:
char holdBuff[4096]; // or some other sufficiently large size
...
*content = malloc (strlen(holdBuff) + 1);
strcpy (*content, holdBuff);
然后您就需要free(content)
在main()
一旦你完成它。
现在,如何真正做到串联:你的表现会更好,如果你忘了使用szBuff
可言,只是直接写入holdBuff
。
char* temp = holdBuff;
while (InternetReadFile(hRequest, temp, 1024, &bytesRead) == TRUE && bytesRead > 0)
{
temp += bytesRead;
}
*temp = '\0'; // manually append NULL terminator
现在holdBuff
将有你想要的,无需中介级联的数据。
工作完美,非常感谢。在过去的一天里,我一直把头发拉出来。 – Lienau 2010-11-10 03:14:51
void urlToChar(char* url, char** content)
{
HINTERNET hConnect = InternetConnect(hSession, _T(""),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = InternetOpenUrl(hSession, url, NULL, 0, 0, 0);
if (hRequest)
{
char holdBuff[] = ""; //do not use fixed size char array and allocate buffer in stack,you can allocate large enough buffer in heap,but recommend you can use string or CString
char szBuff[1025];
memset(szBuff, 0x00, sizeof(szBuff));
DWORD bytesRead;
while (InternetReadFile(hRequest, szBuff, 1024, &bytesRead) == TRUE && bytesRead > 0)
{
// Cat szBuff to holdBuff
//strcat when using char array
// operator+ when using stl string
// [stl string][1]
memset(szBuff, 0x00, sizeof(szBuff));
}
*content = holdBuff;
// memset(holdBuff, 0x00, sizeof(holdBuff)); <-- Need this?
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
}
突出显示您的代码并按下“代码”格式化按钮(1010的东西)。 – chrisaycock 2010-11-10 02:26:22
你得到了什么错误? – chrisaycock 2010-11-10 01:48:45
没有错误,我只是没有找到合适的代码来连接szBuff来holdBuff – Lienau 2010-11-10 02:02:09
啊,现在我明白了为什么你的循环中有一条评论。我已经添加到我的答案。 – chrisaycock 2010-11-10 02:20:45