C 中的 InternetReadFile 到 Char*
我在使用以下代码时遇到了一些问题。我见过大量使用 InternetReadFile 保存到文件的示例。但我找不到一个,或者让它为 char[] 工作。我想添加 szBuffer 以获得holdBuff,然后将内容设置为等于holdBuff。
#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);
}
I'm having a bit of a problem with the following code. I've seen tons of examples for using InternetReadFile to save to a file. But I cant find one, or get it to work for a char[]. I want to add the szBuffer up to get holdBuff, and then set content equal to holdBuff.
#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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
变量声明
将告诉编译器将字符串的内容放入堆栈中。当然,当函数返回时,堆栈就会消失。
因此,在您的情况下:
这告诉编译器创建一个长度为 1 的字符串(NULL 终止符)作为局部变量。仅仅因为您将
content
的值设置为holdBuff
并不意味着holdBuff
所指向的内容不再存在。你必须纠正两件事。首先,您必须使用
strcpy()
或类似函数。其次,您必须为holdBuff
分配足够的空间。示例:
完成后,您需要在
main()
中free(content)
。现在,关于如何实际进行连接:如果您完全忘记使用 szBuff 并直接写入holdBuff,您的性能会好得多。
现在,
holdBuff
将拥有您想要的数据,无需中间连接。The variable declaration
will tell the compiler to put the contents of the string on the stack. Of course, the stack goes away when your function returns.
So in your case:
This tells the compiler to create a string of length one (the NULL terminator) as a local variable. Just because you've set the value of
content
toholdBuff
doesn't mean that whatholdBuff
was pointing exists anymore.You have to correct two things. Firstly, you must use
strcpy()
or similar function. Second, you must allocate sufficient space forholdBuff
.Example:
You'll then need to
free(content)
inmain()
once you're finished with it.Now, for how to actually do the concatenation: Your performance will be much better if you forget about using
szBuff
at all and just write directly toholdBuff
.Now
holdBuff
will have the data you want with no need for intermediary concatenation.