1

I am writing code for http request to get string message. i somehow successful in it. here is my code for http request via curl.

#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;

}

I can get following output for it

<!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 . . .

This is whole web page i just need "hello world" in my output not whole page info how should i can do it with curl.

user3505712
  • 895
  • 1
  • 11
  • 20

1 Answers1

0

Here's some resources that you can pick and select from to craft up your code as you wish:

getinmemory.c shows how you download data directly into a memory buffer of your own.

htmltidy.c shows how you can do another trick and parse the HTML directly at download. This particular example uses LibTidy to do that job but you may of course opt to use something else, parse it yourself.

htmltitle.cpp is a C++ example that uses libxml2 to parse out the <title> of the downloaded page, which also serves as an example of almost what you're asking for.

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222