通过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, 0); 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, 0); if (hRequest == NULL) { return; } DWORD dwTime = 5000; ::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, 0, NULL, 0, strMIME.length(), 0); DWORD dwWritten = 0; 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[1024] = { 0 }; DWORD dwToRead = 1024; DWORD dwHaveRead = 0; ::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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。