http - 从 wxWidgets 应用程序通过 HTTPS POST 发送反馈数据

标签 http ssl curl smtp wxwidgets

我有一个 wxWidgets 应用程序,我想为用户添加一种以简单(实现和使用)、可靠、跨平台和安全的方式提交反馈的方法。在 SSL 上使用 HTTP POST 似乎最符合这些要求(尽管我会考虑建议其他方法的答案)。然而,wxWidgets 对 HTTPS 的支持似乎有限。

以下是我考虑过的一些选项以及它们存在的问题:

  • wxSMTP :我发现没有 SSL/TLS 支持。依赖于具有正确邮件配置(sendmail、MAPI)的用户。
  • wxHTTP : 除了 SSL/HTTPS 支持之外的一切。
  • wxSSL : 一切,如果它不是不完整且早已死去的话。
  • wxCURL : 除了构建/合并复杂之外的一切(事实上,当前版本无法构建)。
  • libcurl : 只需链接并直接调用 libcurl。这是我已经确定的解决方案(并且我有一个工作原型(prototype))但它感觉非常非 wx 并且虽然 libcurl 是跨平台的,但 Windows 绝对不是它的原生平台所以它增加了显着的依赖性和构建复杂性项目.

最佳答案

我决定使用链接到 openssl 的 libCURL。这两个包都可以在大多数 Linux 系统上使用,并且可以相当容易地在 Windows 和 OS X 上构建。

这是发送反馈的示例 C 代码:

#include <stdio.h>
#include <string.h>

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

int main(int argc, char *argv[])
{
  CURL *curl;
  CURLcode res;

  struct curl_httppost *formpost=NULL;
  struct curl_httppost *lastptr=NULL;
  struct curl_slist *headerlist=NULL;
  static const char buf[] = "Expect:";

  curl_global_init(CURL_GLOBAL_ALL);

  /* Fill in the name field */
  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "name",
               CURLFORM_COPYCONTENTS, "John Doe",
               CURLFORM_END);

  /* Fill in the comments field */
  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "comments",
               CURLFORM_COPYCONTENTS, "using HTTPS POST\nline 2\nline 3",
               CURLFORM_END);

  curl = curl_easy_init();
  /* initalize custom header list (stating that Expect: 100-continue is not
     wanted */
  headerlist = curl_slist_append(headerlist, buf);
  if(curl) {
    /* what URL that receives this POST */
    curl_easy_setopt(curl, CURLOPT_URL, 
        "https://some_host.com/path/feedback.php");

    // Uncomment to disable certificate checks
    //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
    //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
    res = curl_easy_perform(curl);

    printf("Curl result: %d\n", res);

    /* always cleanup */
    curl_easy_cleanup(curl);

    /* then cleanup the formpost chain */
    curl_formfree(formpost);
    /* free slist */
    curl_slist_free_all (headerlist);
  }
  return 0;
}

可以像这样在 Linux 上编译:

gcc post.c -lcurl -o post

这是接受该帖子的示例 PHP 代码:

<?php
    $recipient = "john@some_host.com";
    if (empty($_POST)) {
        // We only accpet POSTs
        header('HTTP/1.0 403 Forbidden');
        exit;
    } else {
        // Handle a POST
        $message .= "Submitted at ".date("F j, Y, g:i a")."\n\n";
        $message .= "Name:\n";
        $message .= $_POST['name']."\n\n";
        $message .= "-------------------------------------------\n\n";
        $message .= "Comments:\n\n";
        $message .= $_POST['comments']."\n\n";
        // Send message to email address
        $sent = mail($recipient, "Feedback", 
            $message, "From: Feedback <noreply@some_host.com>\r\n");

        if ($sent) {
?>
            <html>
            <body>
                Got POST and sent email:
                <pre><? echo $message; ?></pre>
            </body>
            </html>
<?php
        } else {
            // Return an error
            header('HTTP/1.0 500 Internal Server Error', true, 500);
            exit;
        }
    }
?>

关于http - 从 wxWidgets 应用程序通过 HTTPS POST 发送反馈数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4356089/

相关文章:

处理超过 3600 秒后,REST API 不返回答案

java - org.apache.jena.atlas.web.HttpException : 405 - HTTP method POST is not supported by this URL

django - 使用 Django 和 nginx 通过个人 SSL 证书登录

ssl - curl:如何为 https 请求指定目标主机名

ruby-on-rails - API 调用在本地工作,但在 Heroku 上不起作用

http - 在 SWI-Prolog 中读取 https 正文

delphi - 使用 Delphi 获取全文提要

java - 使用 SSL 连接的 OpenLdap 失败

Apache VirtualHost : strip www. 并强制 https

xml - 从网站提取足球比分