通过HTTP协议上传文件
一、libcurl
void curl_post_cswuyg()
{
curl_global_init(CURL_GLOBAL_ALL);
CURL* hCurl = curl_easy_init();
if(hCurl != NULL)
{
//也许有Expect: 100-continue,去掉它
curl_slist* pOptionList = NULL;
pOptionList = curl_slist_append(pOptionList, "Expect:");
curl_easy_setopt(hCurl, CURLOPT_HTTPHEADER, pOptionList); curl_httppost* pFormPost = NULL;
curl_httppost* pLastElem = NULL;
//上传文件,指定本地文件完整路径
curl_formadd(&pFormPost, &pLastElem, CURLFORM_COPYNAME, "ufile01", CURLFORM_FILE, "C:\\temp\\upload_test_curl_cswuyg.txt", CURLFORM_CONTENTTYPE, "application/octet-stream", CURLFORM_END); //上传自定义文件内容的文件,CURLFORM_BUFFER指定服务端文件名
//http://curl.haxx.se/libcurl/c/curl_formadd.html
char* file_info = "file_info——cswuyg";
int record_length = std::string(file_info).length();
curl_formadd(&pFormPost, &pLastElem,
CURLFORM_COPYNAME, "ufile01",
CURLFORM_BUFFER, "cswuyg_test.txt",
CURLFORM_BUFFERPTR, file_info,
CURLFORM_BUFFERLENGTH, record_length,
CURLFORM_END);
//不加一个结束的hfs服务端无法写入文件,一般不存在这种问题,这里加入只是为了测试.
//curl_formadd(&pFormPost, &pLastElem, CURLFORM_COPYNAME, "end", CURLFORM_COPYCONTENTS, "end", CURLFORM_END);
curl_easy_setopt(hCurl, CURLOPT_HTTPPOST, pFormPost);
curl_easy_setopt(hCurl, CURLOPT_URL, "http://127.0.0.1/hfs_up/"); CURLcode res = curl_easy_perform(hCurl);
if(res != CURLE_OK)
{
std::wcout << "Error" << std::endl;
}
curl_formfree(pFormPost);
curl_easy_cleanup(hCurl);
} curl_global_cleanup();
}


二、WinHTTP
void cswuyg_winhttp_post()
{
HINTERNET hSession = ::WinHttpOpen(L"cswuyg post test/1.0", WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, NULL);
HINTERNET hConnect = ::WinHttpConnect(hSession, L"127.0.0.1", INTERNET_DEFAULT_HTTP_PORT, );
if (hConnect == NULL)
{
int i = ERROR_WINHTTP_INCORRECT_HANDLE_TYPE;
return;
}
const wchar_t* lpszAcceptedType[] = {L"*/*", NULL};
HINTERNET hRequest = ::WinHttpOpenRequest(hConnect, L"POST", L"hfs_up", L"HTTP/1.1", WINHTTP_NO_REFERER, lpszAcceptedType, );
if (hRequest == NULL)
{
return;
}
DWORD dwTime = ;
::WinHttpSetOption(hRequest, WINHTTP_OPTION_CONNECT_TIMEOUT, &dwTime, sizeof(DWORD));
std::wstring strHeader = L"Content-Type: multipart/form-data; boundary=--boundary_cswuygtest\r\n";
::WinHttpAddRequestHeaders(hRequest, strHeader.c_str(), strHeader.length(), WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE); std::string strMIME = "----boundary_cswuygtest\r\n";
strMIME += "Content-Disposition: form-data; name=\"ufile01\"; filename=\"upload_test_http_cswuyg.txt\"\r\n";
strMIME += "Content-Type:application/octet-stream\r\n\r\n";
strMIME += "1\r\n";
/////////////////////////////
//除了这个表单之外,后头还必须要有另外的表单信息,否则hfs服务端那边无法写入文件,这里仅为测试。
//strMIME += "----boundary_cswuygtest\r\n" ;
//strMIME += "Content-Disposition: form-data; name=\"name\"\r\n\r\n";
//strMIME += "temp.txt\r\n";
/////////////////////////
strMIME += "----boundary_cswuygtest--\r\n";
/////////////////////////
//可以直接写入
//::WinHttpSendRequest(hRequest, strHeader.c_str(), strHeader.length(), (LPVOID)strMIME.c_str(), strMIME.length(), strMIME.length(), 0); //也可以后面分步写入
::WinHttpSendRequest(hRequest, NULL, , NULL, , strMIME.length(), );
DWORD dwWritten = ;
while(!strMIME.empty())
{
::WinHttpWriteData(hRequest, strMIME.c_str(), strMIME.length(), &dwWritten);
if (strMIME.length() > dwWritten)
{
strMIME.substr(dwWritten);
}
else
{
strMIME = "";
} }
::WinHttpReceiveResponse(hRequest, NULL); char buf[] = { };
DWORD dwToRead = ;
DWORD dwHaveRead = ;
::WinHttpReadData(hRequest, buf, dwToRead, &dwHaveRead);
std::cout << buf << std::endl;
::WinHttpCloseHandle(hRequest);
::WinHttpCloseHandle(hConnect);
::WinHttpCloseHandle(hSession);
}


三、总结
MIME:http://baike.baidu.com/view/160611.htm
MIME来源 : Multipurpose Internet Mail Extensions 多用途互联网邮件扩展。服务器会将它们发送的多媒体数据的类型告诉浏览器,而通知手段就是说明该多媒体数据的MIME类型。
GZIP文件:application/x-gzip
任意的二进制数据:application/octet-stream.
普通文本:text/plain

通过HTTP协议上传文件的更多相关文章
- Android用http协议上传文件
http协议上传文件一般最大是2M,比较适合上传小于两M的文件 [代码] [Java]代码 001import java.io.File; 002import java.io.FileInp ...
- c++使用http协议上传文件到七牛云服务器
使用c++ http协议上传文件到七牛服务器时,比较搞的一点就是header的设置: "Content-Type:multipart/form-data;boundary=xxx" ...
- http 协议上传文件multipart form-data boundary 说明--转载
原文地址:http://xixinfei.iteye.com/blog/2002017 含义 ENCTYPE="multipart/form-data" 说明: 通过 http 协 ...
- java http工具类和HttpUrlConnection上传文件分析
利用java中的HttpUrlConnection上传文件,我们其实只要知道Http协议上传文件的标准格式.那么就可以用任何一门语言来模拟浏览器上传文件.下面有几篇文章从http协议入手介绍了java ...
- HTTP协议上传boundary确定&下载content-disposition理解
HTTP协议上传文件-协议 上传文件需要将form标签 的 ENCTYPE 属性设置为 multipart/form-data属性, 与 application/x-www-form-urlencod ...
- libcurl上传文件,添加自定义头
原文 http://www.cnblogs.com/meteoric_cry/p/4285881.html 主题 curl libcurl参数很多,一不小心就容易遇到问题.曾经就遇到过一个很蛋疼的问 ...
- libcurl上传文件
libcurl参数很多,一不小心就容易遇到问题.曾经就遇到过一个很蛋疼的问题:libcurl断点下载>> 这里主要汇总一下,libcurl上传的二种方式: 1.直接上传文件,类似for ...
- 上传文件multipart form-data boundary 说明
含义 ENCTYPE="multipart/form-data" 说明: 通过 http 协议上传文件 rfc1867协议概述,客户端发送内容构造. 概述 ...
- FTP与HTTP上传文件的对比
许多站点,比如facebook或一些博客等都允许用户上传或下载文件,比如论坛或博客系统的图片. 在这种情况下,通常有两种选择上传文件到服务器,那就是FTP协议和HTTP协议. 以下列出了一些两者的不同 ...
随机推荐
- A library of generic data structures
A library of generic data structures including a list, array, hashtable, deque etc.. https://github. ...
- winform控件在Enable=false的情况下改变它的字体颜色
[System.Runtime.InteropServices.DllImport("user32.dll ")] public static extern int ...
- Poco::JSON::Array 中object 设置preserveInsertionOrder 时,stringify出错-->深入解析
在使用poco version 1.6.0时 Poco::JSON::Array 在object 设置preserveInsertionOrder =true 时 调用 array.stringif ...
- 2015-12-23-( dispaly:table的用法)
dispaly属性的table和table-cell属性值不怎么常用,主要是浏览器的兼容性不好,大多数都是为了兼容IE6.IE7,此属性IE8以上,谷歌,火狐,oprea等浏览器都支持. 此disp ...
- slf4j的简单介绍
SLF4J,Simple Logging Facade for JAVA,是一个十分简单的的日志facade,对于不同的日志框架做了一个封装. 对比common logging,Common logg ...
- GeoIP Legacy City数据库安装说明
Here is a brief outline of the steps needed to install GeoIP Legacy City on Linux/Unix. The installa ...
- Hibernate一对多配置
刚刚学习了Hibernate框架的基础知识,下面我来说说关于Hibernate一对多的配置 首先是大配置 连接数据库 用户名 和密码 能和小配置连接 部门小配置: 员工小配置: 部门实体类 员工实体类 ...
- radio相关
radio 按钮组, name=”sex”. <input type="radio" name="sex" value="Male"& ...
- SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/D:/MyEclipseWorkSpace/Emps/WebRoot/WEB-INF/lib/slf4j-nop-1.5.6.
错误的是HQL语句,注意写类名属性名无误,条件无误.
- 免费图片存储和图话【提供demo下载】
我们不管是做博客系统还是其他网站,图片是免不了要使用到的.但是,我们都知道图片的访问是很耗资源的,同时也是很占磁盘空间的,且还特别占带宽. 所以,我们一般都会用到特定的图片服务器.不过,像我等屌丝平时 ...