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

标签 c++ visual-c++ curl httprequest libcurl

我正在为 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/

相关文章:

c++ - 在 C++ Builder 中使用 Ole 试用 Microsoft Word : how to put Word in the foreground

c++ - 如何找到当前目录?

windows - 链接到 User32.dll 时出现链接错误 2001

file - 使用带有 curl 的 bash 脚本通过 FTP 检索目录中最后修改的文件

php - 从 Web 服务器提交时 CURL 错误请求 400

php - 将终端 cURL 转换为 PHP cURL 脚本

python - 在 Cython 中使用 C+ +'s ` str.erase()`

C++函数模板重载

c++ - 这个结构怎么会有 sizeof == 0?

c++ - 使用 boost::math::ibeta 时出错