通过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协议. 以下列出了一些两者的不同 ...
随机推荐
- bzoj1026数位dp
基础的数位dp 但是ce了一发,(abs难道不是cmath里的吗?改成bits/stdc++.h就过了) #include <bits/stdc++.h> using namespace ...
- 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列
第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...
- 使用docker toolbox 在windows上搭建统一环境
1.先下载docker toolbox 以下是下载地址: http://get.daocloud.io/#install-docker-for-mac-windows 2.下载安装 git windo ...
- 关于tableviewcell的一些必备常识
1.设置tableview的背景颜色当设置tableview.backgroundcolor无效时,这样设置: UIView *view = [[UIView alloc] initWithFr ...
- function变量困惑
var name = "The Window"; var object = { name : "My Object", getNameFunc : functi ...
- webform 分页
界面: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx ...
- MySQL字符集
字符集的选择 1.如果数据库只需要支持中文,数据量很大,性能要求也很高,应该选择双字节定长编码的中文字符集(如GBK).因为相对于UTF-8而言,GBK"较小",每个汉字只占2个字 ...
- web图片识别
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- Xcode下搭建OpenGL开发环境
#include <GLUT/GLUT.h> void display() { glClear(GL_ENABLE_BIT); glBegin(GL_POLYGON); glVertex2 ...
- 谢欣伦 - 原创软件 - 工具软件 - 快速关机Shutdown
快速关机Shutdown,含源码. 公司公用的笔记本电脑实在太烂,不知从什么时候开始关机永远都关不了,一直停留在“关闭系统中……”.忍无可忍之下,自己写了一个快速关机程序. 下载: Shutdown_ ...