gpt4 book ai didi

c++ - 如何使用http请求只获取字符串名称

转载 作者:太空宇宙 更新时间:2023-11-04 13:51:09 25 4
gpt4 key购买 nike

我正在为 http 请求编写代码以获取字符串消息。我以某种方式成功了。这是我通过 curl 进行 http 请求的代码。

#include "stdafx.h"
#include <stdio.h>
#include "curl/curl.h"

int main(void)
{
CURL *curl;
CURLcode res;

/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);

/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.1.7:8080/asigra_1/helloword.jsp");
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");

/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));

/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
system("pause");
return 0;

}

我可以得到以下输出

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
g/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hello word

</body>
</html>Press any key to continue . . .

这是整个网页,我只需要在我的输出中显示“hello world”,而不是整个页面信息,我应该如何使用 curl 来实现。

最佳答案

您可以从这里挑选一些资源来根据需要编写代码:

getinmemory.c展示了如何将数据直接下载到您自己的内存缓冲区中。

htmltidy.c展示了如何使用另一个技巧并在下载时直接解析 HTML。这个特定示例使用 LibTidy 来完成这项工作,但您当然可以选择使用其他东西,自己解析。

htmltitle.cpp是一个使用 libxml2 解析出 <title> 的 C++ 示例的下载页面,它也可以作为您所要求的几乎所有内容的示例。

关于c++ - 如何使用http请求只获取字符串名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23334813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com