Dev C ++ Wininet使用HTTP上传文件 [英] Dev C++ Wininet Upload file using HTTP

查看:898
本文介绍了Dev C ++ Wininet使用HTTP上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要上传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\\\
Content-Disposition:form-data; name = \uploadedfile\;; filename = \C:\test.txt\\\\
Content -type:text / plain\\\
\\\
file contents here \\\
----------------------------- 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;
}

//关闭任何有效的internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);

return 0;
}


解决方案



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

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

您必须在这里传递主机名或IP地址,所以localhost ://student114.110mb.com不是。
如果你通过一个URL,你会得到12005错误代码[ msdn上的WinINet错误代码]



另一个问题是frmdata字符串。反斜杠在C:\ test.txt或者你会得到一个制表符字符\t在字符串中。分隔符之前和之后的\\\
也应该被替换为\r\\\
,因为RFC 1521和大多数其他互联网协议使用CRLF作为行分隔符。



这是我使用的字符串。

 code> static TCHAR frmdata [] =----------------------------- 7d82751e2bc0858\r\\\
Content-Disposition :form-data; name = \uploadedfile\; filename = \C:\\test.txt\\r\\\
Content-Type:text / plain\r\\\
\r\\\
file contents here \r\\\
- ---------------------------- 7d82751e2bc0858 - \r\\\
;

最后,PHP代码不工作,因为你使用$ _FILES [file],你应该使用$ _FILES [uploadedfile]。 uploadedfile通常对应于HTML中的< input type =file>标签的名称,但在您的情况下,它在frmdata []字符串的 name = 参数中指定。 / p>

这里是我用来测试这个

的PHP代码

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




  • 写入一个简单的HTML上传表单到
    测试你的php脚本


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



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;
}

解决方案

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

这篇关于Dev C ++ Wininet使用HTTP上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆