0

全部,我在 ASP 中有以下代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <div>
    <form id="Form2" Method="Post" EncType="Multipart/Form-Data" RunAt="Server">

   <%-- The File upload Html control --%>
   Choose Your File  To Upload <BR>
   <Input Name="MyFile" id="MyFile" Type="File" RunAt="Server">
   <BR>

   <%-- A button - when clicked the form is submitted and the
            Upload_Click event handler is fired... --%>
   <Input id="Submit1" Type="Submit" Value="Upload"
             RunAt="Server">

    </form> </div>
</body>
</html>

以及客户端VB.NET中的以下算法:

  1. 将内容类型设置为“multipart/form-data;boundary=---------------------------”+时间戳
  2. 写“\n\r--------------”+时间戳+“\n\r”
  3. 写入 "Content-Disposition: form-data; name="MyFile"; filename="12345.bmp"\r\nContent-Type: image/bitmap\r\n\r\n"
  4. 发布“12345.bmp”的内容

我正在尝试使用 libCURL 将其转换为 C++。

这是我所做的:

        wxString header = wxString::Format( "Content-Disposition: form-data;
name=\"MyFile\"; filename=\"%s\"", fileName );
        post = curl_slist_append( post, "Content-Type: multipart/form-data;
boundary=---------------------------12345" );
        post = curl_slist_append( post, header.c_str() );
        if( !post )
        {
                return;
        }
        post = curl_slist_append( post, "Content-Type: image/bitmap" );
        if( !post )
        {
                curl_slist_free_all( post );
                return;
        }
        result = curl_formadd( &first, &last, CURLFORM_COPYNAME, "name",
CURLFORM_FILE, (const char *) fileName.c_str(), CURLFORM_END );
        result = curl_formadd( &first, &last, CURLFORM_COPYNAME, "filename",
CURLFORM_COPYCONTENTS, (const char *) fileName.c_str(),
CURLFORM_CONTENTHEADER, post, CURLFORM_END );
        result = curl_formadd(&first,  &last, CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "send", CURLFORM_END );
        if( result )
        {
                curl_slist_free_all( post );
                curl_formfree( first );
                return;
        }
        FILE *fp = fopen( "session.log", "w+" );
        if( !fp )
        {
                curl_slist_free_all( post );
                curl_formfree( first );
                return;
        }
        curl_easy_setopt( handle, CURLOPT_VERBOSE, 1L );
        curl_easy_setopt( handle, CURLOPT_HEADER, 1L );
        curl_easy_setopt( handle, CURLOPT_STDERR, fp );
        curl_easy_setopt( handle, CURLOPT_ERRORBUFFER, errorMsg );
        curl_easy_setopt( handle, CURLOPT_URL, "http://xxx.xxx.xxx.xxx/Default.aspx" );
        curl_easy_setopt( handle, CURLOPT_HTTPPOST, first );
        error = curl_easy_perform( handle );
        if( !error )
        {
                curl_slist_free_all( post );
                curl_formfree( first );
                return;
        }
        curl_slist_free_all( post );
        curl_formfree( first );
        curl_easy_setopt( handle, CURLOPT_HTTPPOST, NULL );
        curl_easy_setopt( handle, CURLOPT_HTTPHEADER, NULL );

但不幸的是,它失败了。人们在 Windows 上使用什么来检查数据包的发送方式?或者也许有人可以在我的代码转换中发现问题?

谢谢你。

4

1 回答 1

1

不确定,但通过阅读 wxString 文档,我想知道是否

wxString header = wxString::Format( "Content-Disposition: form-data;
name=\"MyFile\"; filename=\"%s\"", fileName );

不应该

wxString header = wxString::Format( "Content-Disposition: form-data;
name=\"MyFile\"; filename=\"%s\"", fileName.c_str() );

希望这可以帮助。

于 2012-07-26T22:16:11.370 回答