libcurl HTTPS POST数据发送? [英] libcurl HTTPS POST data send?

查看:252
本文介绍了libcurl HTTPS POST数据发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过HTTP POST请求接收数据的应用程序.我正在尝试使用libcurl打开对此应用程序的请求,发送数据并接收来自应用程序的回复.这是我到目前为止的代码:

I have application that received data via HTTP POST requests. I'm trying to use libcurl to open a request to this app, send the data and receive the reply back from the app. This is the code I have so far:

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

    curl = curl_easy_init();
    if(curl) 
    {
        const int timeout = 30000;
        char outputmessage[]="VGhpcyBpcyBqdXN0IGEgbWVzc2FnZSwgaXQncyBub3QgdGhlIHJlYWwgdGhpbmc=";

        curl_easy_setopt(curl, CURLOPT_URL, "https://somehost.com/someapp");
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
        curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.5");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, outputmessage);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(outputmessage));
        curl_easy_setopt(curl, CURLOPT_POST, 1L);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout/1000);

        res = curl_easy_perform(curl);
        if(CURLE_OK != res)
        {
            printf("Error: %s\n", strerror(res));
            return 1;
        }

        // now what?

        // cleanup when done
        curl_easy_cleanup(curl);
    }
    return 0;
}

这实际上连接到服务器,我在curl_easy_perform()上获得了确定.我可以在Wireshark上看到连接.

This essentially connects to the server, I get a OK on the curl_easy_perform(). I can see the connection on Wireshark.

两个问题:

1)这是通过POST发送数据的正确方法吗?
2)如何从应用程序中获取答复.

1) Is this the correct way of sending data via a POST, and
2) How do I get the reply from the application.

我以为我需要使用curl_easy_recv(),但是我看不到如何使用.欢迎任何示例代码. 谢谢.

I thought I need to use curl_easy_recv() but I can't see how. Any sample code is welcome. Thanks.

推荐答案

我认为您还应该考虑为响应标头添加处理程序:

I think you should also consider adding a handler for the response headers:

cres = curl_easy_setopt( handle, CURLOPT_HEADERFUNCTION, HandleHeader );

如果这是设计供应用程序使用的Web服务,则其答复可能是仅标头的答复,没有响应数据.标头将包含以下数据:

It may be that if this is a web service designed for use by an application, its reply is a header-only reply, with no response data. The header would contain data such as:

HTTP/1.1 200 OK ...

您要查找的主要内容是200代码,表明请求正常.

Where the main thing you're looking for is the 200 code saying the request was OK.

用于处理标头或响应中的数据的函数应为void *,并且如果将其用作字符串,则需要将其以null终止.有关信息,这是我处理传入数据的方式(在这种情况下,我仅在应用程序中使用返回码-响应主体仅进入日志文件):

The functions to handle the data from either header or response should take a void*, and you'll need to null-terminate it if you're using it as a string. For info this is how I handle the incoming data (in this case I only use the return code in my application - the response body just goes to a log file):

static size_t CurlHandleResponse(
  void *Ptr,
  size_t Size,
  size_t NoElements,
  void* Data )
{
  memcpy( &( gData[gDataLen] ), Ptr, (Size * NoElements) );
  gDataLen += (Size * NoElements);
  gData[ gDataLen ] = '\0';
  return (Size * NoElements);
}

...
ConvertUnprintable( gData, PrintStr );
...

为简洁起见,已删除错误处理!

Error handling removed for brevity!

这篇关于libcurl HTTPS POST数据发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆