C 中的 InternetReadFile 到 Char*

发布于 2024-10-01 03:06:43 字数 1375 浏览 0 评论 0原文

我在使用以下代码时遇到了一些问题。我见过大量使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听不够的曲调 2024-10-08 03:06:43

变量声明

char xyz[] = "Hello World!";

将告诉编译器将字符串的内容放入堆栈中。当然,当函数返回时,堆栈就会消失。

因此,在您的情况下:

char holdBuff[] = "";
...
*content = holdBuff;

这告诉编译器创建一个长度为 1 的字符串(NULL 终止符)作为局部变量。仅仅因为您将 content 的值设置为 holdBuff 并不意味着 holdBuff 所指向的内容不再存在。

你必须纠正两件事。首先,您必须使用 strcpy() 或类似函数。其次,您必须为 holdBuff 分配足够的空间。

示例:

char holdBuff[4096];    // or some other sufficiently large size
...
*content = malloc (strlen(holdBuff) + 1);
strcpy (*content, holdBuff);

完成后,您需要在 main()free(content)


现在,关于如何实际进行连接:如果您完全忘记使用 szBuff 并直接写入holdBuff,您的性能会好得多。

char* temp = holdBuff;
while (InternetReadFile(hRequest, temp, 1024, &bytesRead) == TRUE && bytesRead > 0)
{
    temp += bytesRead;
}
*temp = '\0';    // manually append NULL terminator

现在,holdBuff 将拥有您想要的数据,无需中间连接。

The variable declaration

char xyz[] = "Hello World!";

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:

char holdBuff[] = "";
...
*content = holdBuff;

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 to holdBuff doesn't mean that what holdBuff was pointing exists anymore.

You have to correct two things. Firstly, you must use strcpy() or similar function. Second, you must allocate sufficient space for holdBuff.

Example:

char holdBuff[4096];    // or some other sufficiently large size
...
*content = malloc (strlen(holdBuff) + 1);
strcpy (*content, holdBuff);

You'll then need to free(content) in main() 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 to holdBuff.

char* temp = holdBuff;
while (InternetReadFile(hRequest, temp, 1024, &bytesRead) == TRUE && bytesRead > 0)
{
    temp += bytesRead;
}
*temp = '\0';    // manually append NULL terminator

Now holdBuff will have the data you want with no need for intermediary concatenation.

给不了的爱 2024-10-08 03:06:43
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);
}
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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文