MFC POST 文件到 http服务器
来源:互联网 发布:java的变量使用范围 编辑:程序博客网 时间:2024/06/06 05:50
include <afxinet.h>,关键是要注意http的post协议格式,以及编码(Unicode下需要进行转换)。主要分为以下几个步骤:
1、 编写数据包的头:
这里需要注意一个是”Accept: audio/x-wav,text/plain,*/*;….”表示的是希望服务器接收的数据格式;还有就是”Content-Type: multipart/form-data;boundary=\%s”,表示是上传文件,boundary只是划分的分隔符(一般以”------------------7b1ac890a1b”的形式存在);
2、 编写传输的主数据体:
可以理解为将不同的数据通过boundary分割开来,每类数据分别发送。首行的boundary前面需要多加”--”。格式为:
--------------------7b1ac890a1b
Content-Disposition:form-data; name=”name”; filename=”filename”
Content-Type: audio/x-wav
....(wav的byte流数据)
3、 编写数据包尾:
数据包结尾最后一个boundary后要多加”--”。格式为:
--------------------7b1ac890a1b
Content-Disposition:form-data; name=”Submit”
Submit
--------------------7b1ac890a1b--
下面是代码:
void CCPostDlg::HttpPostFile( CString url, CString file, CString paramName, CString contentType ){ CString boundary = "---------------------------1a2b3c4d5e6f";//分隔符,注意:首行必须多"--",尾部最后必须加"--",否则识别不出。 CString boundaryTemplate; boundaryTemplate.Format(_T("\r\n--%s\r\n") , boundary); byte* boundarybytes = (byte*)boundaryTemplate.GetBuffer(boundaryTemplate.GetLength()); CInternetSession pSession("ic_PostWav"); CHttpConnection* pConnect; CHttpFile* pFile; CString pServeIP = _T("192.168.10.156"); INTERNET_PORT wPort = 80; CString pObject = _T("/yxbp/index.php"); pConnect= pSession.GetHttpConnection(pServeIP , wPort); pFile= pConnect->OpenRequest(CHttpConnection::HTTP_VERB_POST, pObject ,NULL,0,NULL,NULL,INTERNET_FLAG_DONT_CACHE); //数据包头 CString pPostHeader , pPostHeaderTemplate; pPostHeaderTemplate= _T("Accept:audio/x-wav,text/html,application/xhtml+xml,application/xml,*/*;q=0.9,*/*;q=0.8\r\n") _T("Content-Type: multipart/form-data;boundary=\%s\r\n") _T("Connection: keep-alive\r\n"); pPostHeader.Format(pPostHeaderTemplate, boundary); pFile->AddRequestHeaders(pPostHeader); //数据帧头 CString pPostTop , pPostTopTemplate; pPostTopTemplate= _T("\%sContent-Disposition:form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: \%s\r\n\r\n"); pPostTop.Format(pPostTopTemplate , boundaryTemplate,_T("file"), file , contentType); byte* pPostTopbytes = (byte*)pPostTop.GetBuffer(pPostTop.GetLength()); //数据包尾 CString ender,enderTemplate; enderTemplate= _T("\r\n--\%s\r\nContent-Disposition:form-data; name=\"\%s\"\r\n\r\n\%s"); ender.Format(enderTemplate, boundary , _T("Ender") , _T("ender")); ender+= _T("\r\n--"); ender+= boundary; ender+= _T("--\r\n"); byte* enderbyte = (byte*)ender.GetBuffer(ender.GetLength()); CFile cfile; cfile.Open(file , CFile::modeRead|CFile::shareDenyRead, NULL); DWORD dwSize = pPostTop.GetLength() + ender.GetLength() + cfile.GetLength(); pFile->SendRequestEx(dwSize); pFile->Write(pPostTopbytes, pPostTop.GetLength()); //数据主体 int bufflength = 4 * 1024; byte* buffer = newbyte[bufflength]; int byteRead = 0; while((byteRead = cfile.Read(buffer , bufflength)) != 0) { pFile->Write(buffer , byteRead); } cfile.Close(); //写尾 pFile->Write(enderbyte, ender.GetLength()); pFile->EndRequest(); CString strSentence = "",strGetSentence = ""; DWORD dwRet ; pFile->QueryInfoStatusCode(dwRet); if(HTTP_STATUS_OK==dwRet) { while(pFile->ReadString(strSentence)) // 读取提交数据后的返回结果 { strGetSentence = strGetSentence+ strSentence ; } } pFile->Close(); pConnect->Close();}
调用:
HttpPostFile("http://127.0.0.1/xxxx/index.php","C:\\temp.wav" , "file","audio/x-wav");
- MFC POST 文件到 http服务器
- http post 文件 到服务器
- 通过 HTTP POST 上传文件到服务器.
- 通过 HTTP POST 上传文件到服务器.
- 通过 HTTP POST 上传文件到服务器
- VC http post 文件到服务器
- 将文件通过http的post方法上传到服务器
- Http工具类 同时 post上传 多文件到服务器
- Java客户端通过Http发送POST请求上传文件到web服务器
- IOS POST 数据,包括文件 到服务器
- Android Post文件到服务器问题记录
- android post上传文件到服务器
- NoHttp的post上传文件到服务器
- Qt Post上传图片文件到服务器
- libevent http post 服务器
- Applet 通过http上传文件到服务器
- vc 保存http服务器文件到本地
- 使用HTTP Client 上传文件到服务器
- 主分区,扩展分区,逻辑分区的理解
- Flex + java + orcale 存取Image
- 寒假-05,贪吃蛇
- STL list链表的用法详解
- Hello CSDN
- MFC POST 文件到 http服务器
- MySQL表最大能达到多少?
- Android面试题之二(中)
- C#.net访问web URL并处理返回值 && 不加web引用调用webservice
- .NET 强制退出WinForm程序 C#+.NET
- winform最小化后隐藏到右下角,单击或双击后恢复
- 我的第一个C++程序
- 如何用C#语言构造蜘蛛程序
- [python] os.walk()与os.path.walk()