c++ - 有没有使用 WinInet C++ 库的 http 上传的好例子

标签 c++ http upload wininet

我无法让我的代码工作:/

最佳答案

最终我在 web 上找到了一些工作示例

static char szRawData[5000];
  memset(szRawData, 0x11, sizeof(szRawData));

  //
  // CIHandle is just a wrapper class for HINTERNET, that closes handle in destructor
  //
  CIHandle hIntrn = InternetOpen( "LiveUpdate"), 
                                  INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY,
                                  NULL,
                                  NULL,
                                  0);
  if (!hIntrn)
    return printf("No Internet connection: %li.\n", GetLastError());

  CIHandle hConn = InternetConnect( hIntrn, 
                                    "65.254.250.104",
                                    INTERNET_DEFAULT_HTTP_PORT,
                                    NULL,
                                    NULL,
                                    INTERNET_SERVICE_HTTP,
                                    0,
                                    NULL);
  if (!hConn)
    return printf("Connection to update server failed: %li.\n", GetLastError());

  DWORD dwOpenRequestFlags = INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
                             INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS |
                             INTERNET_FLAG_KEEP_CONNECTION |
                             INTERNET_FLAG_NO_AUTO_REDIRECT |
                             INTERNET_FLAG_NO_COOKIES |
                             INTERNET_FLAG_NO_CACHE_WRITE |
                             INTERNET_FLAG_NO_UI |
                             INTERNET_FLAG_RELOAD;

  CIHandle hReq = HttpOpenRequest(hConn,
                                  "POST",
                                  "upload.php",
                                  "HTTP/1.0",
                                  NULL,
                                  NULL,
                                  dwOpenRequestFlags,
                                  NULL);

  ZString strBoundary = "---------------------------autoupdater";
  ZString strContentHeader =  "Host: www.mydomain_at_powweb.com\r\n"
                              "Content-Type: multipart/form-data; boundary=";
  strContentHeader+=strBoundary;

  HttpAddRequestHeaders(hReq, strContentHeader, strContentHeader.length(), HTTP_ADDREQ_FLAG_ADD);


  ZString strHeaders;
  strHeaders.precache(16384);
  sprintf(strHeaders,
          "--%s\r\n"
          "Content-Disposition: form-data; name=\"userfile\"; "
          "filename=\"test.raw\"\r\n"
          "Content-Type: application/octet-stream\r\n\r\n",
          (LPCTSTR)strBoundary);

  tCharSeq s;//this is a just a dynamic array of bytes;
  //
  // append headers and file to request:
  //
  s.precache(16384);
  s.append(strHeaders.length(), strHeaders);
  //append with file data:
  s.append(2000, szRawData); //<------------------- depending on this size, SendRequest fails.
  //trailing end of data:
  s.append(4,"\r\n--");
  s.append(strBoundary.length(), (LPTSTR)strBoundary);
  s.append(4,"--\r\n");

  InternetSetOption(hReq, INTERNET_OPTION_USERNAME, "username\0", 9);
  InternetSetOption(hReq, INTERNET_OPTION_PASSWORD, "password\0", 9);

  if (!HttpSendRequest(hReq, NULL, 0, (void*)s.getBuffer(), s.length()))
    return printf("HttpSendRequest failed: %li.\n", GetLastError());

关于c++ - 有没有使用 WinInet C++ 库的 http 上传的好例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/471198/

相关文章:

node.js - 使用Mongoose查询数据时如何显示Multer上传的图片

qt - 在 Qt4 上使用 POST 方法上传文件

c++ - 如何从 native 代码设置 TextBox 控件中的文本?

c++ - 更有效地在微 Controller 上对 C++ 进行基准测试

javascript - 不使用 JSONP 对不同域的 AJAX 调用

php - 可能 "undo"在 PHP 脚本中发送 HTTP header 和此类中间执行?

javascript - 在将文件传递给 Javascript 函数之前,如何通过 HTML 上传文件?

c++ - 为什么在使用 char 指针 str1 而不是 str 时会出现分段转储?

c++ - 为什么 GCC 调用 libc 的 sqrt() 而不使用它的结果?

javascript - 检测来自 iframe 的 http 请求