MFC POST 檔案到 http伺服器
阿新 • • 發佈:2018-12-31
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");