cURL / Mailing Lists / curl-users / Single Mail

curl-users

send data over socket using libcurl

From: Dhiren <simplydhirendra_at_gmail.com>
Date: Wed, 7 Jan 2009 12:49:00 +0530

hi all,

1. i had used diffrent formate to send data .
//char *request = "GET https://202.189.252.134:80 HTTP/1.1\r\n";
 char *request = "GET / HTTP/1.1\r\nHost: 202.189.252.134\r\nConnection:
Close\r\n\r\n";
// char *request = "connect to server.";

for curl_easy_send. and it's return CURLE_OK. but getting problem in
curl_easy_recv
it's return proxy error :
HTTP/1.1 502 Proxy Error ( The Uniform Resource Locator (URL) does not use a
recognized protocol. Either the protocol is not supported or the request was
not typed correctly. Confirm that a valid protocol is in use (for example,
HTTP for aWeb request). )

2. when using
        char *request = "connect to server."; // for send buffer
and at server a application listning the port 443 and when get connect send
a response on same socket.
then getting response null curl_easy_recv. and CURLcode = CURLE_AGAIN

so please give me the solution where i am going wrong.
i think problem is related to send buffer text of curl_easy_send.

please help me
it's very urgent .

thanks

code:

int main(int argc, char* argv[])
{
// Our curl objects
    CURL *curl;
    CURLcode result;
 CURLcode res;
 char * cProxyUser ="teraltd.com\\dhirendra";
 char * cProxyPass ="maa";
 char * strVersion;
 //char *request = "GET https://202.189.252.134:80 HTTP/1.1\r\n";
 char *request = "GET / HTTP/1.1\r\nHost: 202.189.252.134\r\nConnection:
Close\r\n\r\n";
// char *request = "connect to server.";
 int sockfd; /* socket */
 size_t iolen;

    // Create our curl handle
    curl = curl_easy_init();
 strVersion = curl_version( );
 //printf("CURL Version info:%s\n",strVersion);
    if (curl)
    {

      // Now set up all of the curl options
   curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
      curl_easy_setopt(curl, CURLOPT_URL, "202.189.252.134");
   curl_easy_setopt(curl, CURLOPT_PORT, 80);
      //curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);
      //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
      //curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
    //------------
   curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1);
   // Proxy Details
   curl_easy_setopt(curl, CURLOPT_PROXY, "192.168.50.10");
   curl_easy_setopt(curl, CURLOPT_PROXYPORT , 8080);
   curl_easy_setopt(curl, CURLOPT_PROXYTYPE ,CURLPROXY_HTTP);
   curl_easy_setopt(curl, CURLOPT_PROXYAUTH ,CURLOPT_HTTPAUTH );
   //curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD,cProxyUser );
    curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,cProxyUser );
   curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD ,cProxyPass );

      // Attempt to Connect the remote server.
      result = curl_easy_perform(curl);

      // Did we succeed?
      if (result == CURLE_OK)
      {
        cout << buffer << "\n";

      }
      else
      {
        cout << "Error: [" << result << "] - " << errorBuffer;

      }
   curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  /* Extract the socket from the curl handle - we'll need it
     * for waiting */
    res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockfd);
 //curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 0);
    if(CURLE_OK != res)
    {
      printf("Error: %s\n", strerror(res));
      return 1;
    }

  /* wait for the socket to become ready for sending */
    if(!wait_on_socket(sockfd, 0, 60000L))
    {
      printf("Error: timeout.\n");
      return 1;
    }

    puts("Sending request.");
    /* Send the request. Real applications should check the iolen
     * to see if all the request has been sent */
    res = curl_easy_send(curl, request, strlen(request), &iolen);

    if(CURLE_OK != res)
    {
      printf("Error: %s\n", strerror(res));
      return 1;
    }
 long rescode;
 res = curl_easy_getinfo(curl, CURLINFO_HTTP_CONNECTCODE ,&rescode );

    puts("Reading response.");

    /* read the response */
    for(;;)
   {
      char buf[1024];

      wait_on_socket(sockfd, 1, 60000L);
      res = curl_easy_recv(curl, buf, 1024, &iolen);

      if(CURLE_OK != res)
   {
    puts("connection Faild....");
        break;
   }
   if(CURLE_AGAIN ==res)
    puts(" call return CURLE_AGAIN - the socket is used in non-blocking
mode internally");

   if(CURLE_OK == res)
   puts(buf);
   }

   puts(errorBuffer);
    /* always cleanup */
    curl_easy_cleanup(curl);

    }
 getch();
  return -1;
}

-- 
Thanks and Regards
Dhirendra Kumar
Software Engineer
Tera Informatics Pvt. Ltd.
111, SDF - 4, Seepz
Andhari(E) , Mumbai
Mob - 9967462468
           9324917396

-------------------------------------------------------------------
List admin: http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-users
FAQ: http://curl.haxx.se/docs/faq.html
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2009-01-07