开发 C++ Wininet 使用 HTTP 上传文件

发布于 2024-08-16 10:09:05 字数 1966 浏览 11 评论 0原文

我想将“C:\test.txt”上传到网络服务器,当我运行程序时,文件没有上传,并且没有收到任何错误。

完整的 C++ 代码可以在这里找到

,网络服务器上的 php 代码可以在这里找到: “http://student114.110mb.com/upload.txt" 或者 “http://student114.110mb.com/upload.php

请帮助我我做错了

#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#include <iostream>

#pragma comment(lib,"wininet.lib")

using namespace std;

int main()
{

    static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\test.txt\"\nContent-Type: text/plain\n\nfile contents  here\n-----------------------------7d82751e2bc0858--"; 
    static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"; 

    HINTERNET hSession = InternetOpen("MyAgent",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
     if(hSession==NULL)
    {
     cout<<"Error: InternetOpen";  
    }


    HINTERNET hConnect = InternetConnect(hSession, _T("localhost"),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
     if(hConnect==NULL)
    {
     cout<<"Error: InternetConnect";  
    }

    HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T("upload.php"), NULL, NULL, (const char**)"*/*\0", 0, 1);
    if(hRequest==NULL)
    {
     cout<<"Error: HttpOpenRequest";  
    }

    BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
    if(!sent)
    {
     cout<<"Error: HttpSendRequest";
     }

    //close any valid internet-handles
    InternetCloseHandle(hSession);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hRequest);

    return 0;
}

I want to upload "C:\test.txt" to webserver, when I am running program, file is not uploading and I am not getting any error.

the complete C++ code can be find here

and php code on webserver can be find here: "http://student114.110mb.com/upload.txt"
or
"http://student114.110mb.com/upload.php"

kindly help me where I am doing wrong

#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#include <iostream>

#pragma comment(lib,"wininet.lib")

using namespace std;

int main()
{

    static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\test.txt\"\nContent-Type: text/plain\n\nfile contents  here\n-----------------------------7d82751e2bc0858--"; 
    static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"; 

    HINTERNET hSession = InternetOpen("MyAgent",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
     if(hSession==NULL)
    {
     cout<<"Error: InternetOpen";  
    }


    HINTERNET hConnect = InternetConnect(hSession, _T("localhost"),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
     if(hConnect==NULL)
    {
     cout<<"Error: InternetConnect";  
    }

    HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T("upload.php"), NULL, NULL, (const char**)"*/*\0", 0, 1);
    if(hRequest==NULL)
    {
     cout<<"Error: HttpOpenRequest";  
    }

    BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
    if(!sent)
    {
     cout<<"Error: HttpSendRequest";
     }

    //close any valid internet-handles
    InternetCloseHandle(hSession);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hRequest);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

汐鸠 2024-08-23 10:09:05

我能够使你的代码工作。

首先,您提供的链接上的代码和您发布的代码不同:

InternetConnect(hSession, _T("localhost"), ...
InternetConnect(hSession, _T("http://student114.110mb.com"), ...

您必须在此处传递主机名或 IP 地址,因此“localhost”很好,但“http://student114.110mb.com" 不是。
如果您传递 URL,您将收到 12005 错误代码 [请参阅 msdn 上的 WinINet 错误代码]

另一个问题是 frmdata 字符串。您应该在 C:\test.txt 中加倍反斜杠,否则您的字符串中将出现一个制表符 \t。分隔符前后的 \n 也应替换为 \r\n,因为 RFC 1521 和大多数其他 Internet 协议使用 CRLF 作为行分隔符。

这是我使用过的字符串。

static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents  here\r\n-----------------------------7d82751e2bc0858--\r\n";

最后,PHP 代码不起作用,因为您在应该使用 $_FILES["uploadedfile"] 的地方使用了 $_FILES["file"]。 “uploadedfile”通常对应于文件的名称。 HTML 中的标记,但在您的情况下,它是在 frmdata[] 字符串的 name= 参数中指定的。

这是我用来测试这个的 PHP 代码。

move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "/files/my_file");

当您处理像这样复杂的客户端/服务器交互时,它有助于单独测试每个部分。例如,你可以。

  • 编写一个简单的 HTML 上传表单
    测试您的 php 脚本

  • 让您的程序将其请求发送到
    netcat 并检查输出

I was able to make your code work.

First of all the code on the link you provided and the code you posted is not the same:

InternetConnect(hSession, _T("localhost"), ...
InternetConnect(hSession, _T("http://student114.110mb.com"), ...

You must pass an host name or ip address here so "localhost" is good but "http://student114.110mb.com" isn't.
If you pass an URL you will get the 12005 error code [see WinINet error codes on msdn].

Another problem is the frmdata string. You should double the backslash in C:\test.txt or you will get a tab character \t in your string. The \n before and after the delimiters should also be replaced by \r\n because RFC 1521 and most other internet protocols use CRLF as a line delimiter.

Here is the string I have used.

static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents  here\r\n-----------------------------7d82751e2bc0858--\r\n";

Finally the PHP code doesn't work because you use $_FILES["file"] where you should be using $_FILES["uploadedfile"]. "uploadedfile" would typically correspond to the name of an <input type="file"> tag in HTML but in your case it is specified in the name= parameter of the frmdata[] string.

Here's the PHP code I have used to test this

move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "/files/my_file");

When you work on complex client/server interaction like this it helps to test each part separately. You could for instance.

  • Write a simple HTML upload form to
    test your php script

  • Have your program send its request to
    netcat and examine the output

一笔一画续写前缘 2024-08-23 10:09:05

如果您想通过 php 脚本上传文件的良好请求,则必须发送不带 WinAPI 的完整请求。

过去,我也尝试过像你一样发送这样的请求,以及所有的api函数。
但是当我用wireshark嗅探它时,我得到了和你一样的解决方案:标头被合并,但它不起作用。

所以我再次使用了winsock,如果你要发送的数据不太大,它就可以工作。

代码如下:

#include <iostream>
#include <winsock2.h>
#include <string>
using namespace std;

//link libwsock32.a

unsigned long WinsockStart()
{
     WSADATA wsa;
     unsigned long ulong;
     struct hostent *host;

     if(WSAStartup(MAKEWORD(2,2), &wsa) < 0)
     {
          cout << "Error WinsockStart()" << endl;
          WSACleanup();
          return 1;
          }

     if((host=gethostbyname("www.example.com"))<0)
     {
          cout << "Fehler gethostbyname()" << endl;
          WSACleanup();
          return 2;
          }

     ulong = *(unsigned long*) host->h_addr;

     return ulong;
      }

     void error_exit(string text)
     {
     cout << text;
     WSACleanup();
     exit(EXIT_FAILURE);
      }



int main()
{
SOCKET sock;
struct sockaddr_in addr;
unsigned long win=0;
int con = 0, gr=0, send_r=0, rec=0;
char header[2048], puffer[2018]; 
string to_send="hello world";
string name="test99.txt";

win=WinsockStart();
   if(win==1||win==2)
      error_exit("Error WinsockStart()");

addr.sin_family=AF_INET;
addr.sin_port=htons(80);
addr.sin_addr.s_addr = win;

sock = socket(AF_INET, SOCK_STREAM, 0);
   if(sock<0)
      error_exit("Error socket()");

gr = (to_send.size()+name.size()+287);

sprintf(header, "POST /upload.php HTTP/1.1\r\n");
sprintf(header, "%sHost: www.example.com\r\n", header);
sprintf(header, "%sConnection: Keep-Alive\r\n", header);
sprintf(header, "%sContent-Type: multipart/form-data; boundary=---------------------------90721038027008\r\n", header);
sprintf(header, "%sContent-Length: %d\r\n", header, gr);
sprintf(header, "%s\r\n", header);
sprintf(header, "%s-----------------------------90721038027008\r\n", header);
sprintf(header, "%sContent-Disposition: form-data; name=\"upfile\"; filename=\"%s\"\r\n", header, name.c_str());
sprintf(header, "%sContent-Type: text/plain\r\n", header);
sprintf(header, "%s\r\n", header);
sprintf(header, "%s%s\r\n", header, to_send.c_str());
sprintf(header, "%s-----------------------------90721038027008\r\n", header);
sprintf(header, "%sContent-Disposition: form-data; name=\"post\"\r\n", header);
sprintf(header, "%s\r\n", header);
sprintf(header, "%supload\r\n\r\n", header);
sprintf(header, "%s-----------------------------90721038027008--\r\n\r\n\0", header);      

con = connect(sock, (SOCKADDR*)&addr, sizeof(addr));
   if(con < 0)
      error_exit("Error connect()");

if(send_r=send(sock, header, strlen(header), 0)<0)
      error_exit("Error send()");

 while(rec=recv(sock, puffer, 2048, 0))
 {
  if(rec==0)
    error_exit("Server quit");

 printf("%s", puffer);
 }               

closesocket(sock);
WSACleanup();
return EXIT_SUCCESS;
}

到目前为止,我已经搜索并尝试了使用 WinHttpAPI 的其他解决方案,但没有找到任何适合我目的的解决方案。

赞助

人抱歉我的英语不好,这不是我的母语

you have to send a full request without the WinAPI if you want to have a good request for uploading a file over a php script.

In the past, I also tried to send such a request like you, with all the api functions.
But when I sniffed it with wireshark, I got the same solution like you: the header is merged and it didn't work.

So I used winsock again and it worked if the data which you want to send aren't too big.

here's the code:

#include <iostream>
#include <winsock2.h>
#include <string>
using namespace std;

//link libwsock32.a

unsigned long WinsockStart()
{
     WSADATA wsa;
     unsigned long ulong;
     struct hostent *host;

     if(WSAStartup(MAKEWORD(2,2), &wsa) < 0)
     {
          cout << "Error WinsockStart()" << endl;
          WSACleanup();
          return 1;
          }

     if((host=gethostbyname("www.example.com"))<0)
     {
          cout << "Fehler gethostbyname()" << endl;
          WSACleanup();
          return 2;
          }

     ulong = *(unsigned long*) host->h_addr;

     return ulong;
      }

     void error_exit(string text)
     {
     cout << text;
     WSACleanup();
     exit(EXIT_FAILURE);
      }



int main()
{
SOCKET sock;
struct sockaddr_in addr;
unsigned long win=0;
int con = 0, gr=0, send_r=0, rec=0;
char header[2048], puffer[2018]; 
string to_send="hello world";
string name="test99.txt";

win=WinsockStart();
   if(win==1||win==2)
      error_exit("Error WinsockStart()");

addr.sin_family=AF_INET;
addr.sin_port=htons(80);
addr.sin_addr.s_addr = win;

sock = socket(AF_INET, SOCK_STREAM, 0);
   if(sock<0)
      error_exit("Error socket()");

gr = (to_send.size()+name.size()+287);

sprintf(header, "POST /upload.php HTTP/1.1\r\n");
sprintf(header, "%sHost: www.example.com\r\n", header);
sprintf(header, "%sConnection: Keep-Alive\r\n", header);
sprintf(header, "%sContent-Type: multipart/form-data; boundary=---------------------------90721038027008\r\n", header);
sprintf(header, "%sContent-Length: %d\r\n", header, gr);
sprintf(header, "%s\r\n", header);
sprintf(header, "%s-----------------------------90721038027008\r\n", header);
sprintf(header, "%sContent-Disposition: form-data; name=\"upfile\"; filename=\"%s\"\r\n", header, name.c_str());
sprintf(header, "%sContent-Type: text/plain\r\n", header);
sprintf(header, "%s\r\n", header);
sprintf(header, "%s%s\r\n", header, to_send.c_str());
sprintf(header, "%s-----------------------------90721038027008\r\n", header);
sprintf(header, "%sContent-Disposition: form-data; name=\"post\"\r\n", header);
sprintf(header, "%s\r\n", header);
sprintf(header, "%supload\r\n\r\n", header);
sprintf(header, "%s-----------------------------90721038027008--\r\n\r\n\0", header);      

con = connect(sock, (SOCKADDR*)&addr, sizeof(addr));
   if(con < 0)
      error_exit("Error connect()");

if(send_r=send(sock, header, strlen(header), 0)<0)
      error_exit("Error send()");

 while(rec=recv(sock, puffer, 2048, 0))
 {
  if(rec==0)
    error_exit("Server quit");

 printf("%s", puffer);
 }               

closesocket(sock);
WSACleanup();
return EXIT_SUCCESS;
}

Until now, I've searched and I've tried for other solutions using the WinHttpAPI, but I didn't find anything for my purposes.

Patrone

P.S. Sorry for my bad english, it's not my native language

り繁华旳梦境 2024-08-23 10:09:05
static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents  here\r\n-----------------------------7d82751e2bc0858--\r\n";

最后一个“--”字符应该位于“----------------------------7d82751e2bc0858”边界之前,而不是之后。

正确代码:

static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents  here\r\n-------------------------------7d82751e2bc0858\r\n";
static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents  here\r\n-----------------------------7d82751e2bc0858--\r\n";

Last "--" characters should be before the "-----------------------------7d82751e2bc0858" boundary, not after it.

Correct code:

static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents  here\r\n-------------------------------7d82751e2bc0858\r\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文