2

「C:\ test.txt」をウェブサーバーにアップロードしたいのですが、プログラムを実行しているときにファイルがアップロードされず、エラーが発生しません。

完全なC++コードはここにあります

Webサーバーの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;
}
4

3 に答える 3

5

私はあなたのコードを機能させることができました。

まず、提供したリンクのコードと投稿したコードは同じではありません。

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

ここでホスト名または IP アドレスを渡す必要があるため、"localhost" は適切ですが、" http://student114.110mb.com " は適切ではありません。URL を渡すと、エラー コード 12005 が返されます [ msdn の WinINet エラー コードを参照してください]

もう 1 つの問題は、frmdata 文字列です。C:\test.txt のバックスラッシュを 2 つにする必要があります。そうしないと、文字列にタブ文字 \t が含まれます。デリミタの前後の \n も \r\n に置き換える必要があります。これは、RFC 1521 および他のほとんどのインターネット プロトコルが 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";

最後に、$_FILES["uploadedfile"] を使用する必要がある場所で $_FILES["file"] を使用しているため、PHP コードが機能しません。「uploadedfile」は通常、HTML の <input type="file"> タグの名前に対応しますが、あなたの場合は frmdata[] 文字列のname=パラメータで指定されています。

これをテストするために使用したPHPコードは次のとおりです

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

このような複雑なクライアント/サーバーのやり取りを行う場合、各部分を個別にテストすると役立ちます。たとえば、できます。

  • 簡単な HTML アップロード フォームを作成して、PHP スクリプトをテストする

  • プログラムでリクエストを netcat に送信し、出力を調べます。

于 2009-12-31T21:00:03.793 に答える
2

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 を使用して他のソリューションを検索し、試してみましたが、目的に合ったものは見つかりませんでした。

パトロン

PS下手な英語で申し訳ありません、それは私の母国語ではありません

于 2010-03-31T16:42:18.417 に答える
1
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";
于 2015-01-09T14:17:25.367 に答える