码迷,mamicode.com
首页 > 编程语言 > 详细

Windows MFC HTTP POST请求 函数流程

时间:2019-09-25 12:59:29      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:exce   %x   res   --   length   text   cstring   service   net   

Windows MFC HTTP POST请求 函数流程

  1 CString m_strHttpUrl(_T("http://10.200.80.86:8090/course/upload"))
  2 CInternetSession * pInternetSession = NULL;
  3 CHttpConnection * pHttpConnection = NULL;
  4 CHttpFile * pHttpFile = NULL;
  5 
  6 
  7 
  8 //建立连接
  9 pInternetSession = new CInternetSession(AfxGetAppName());
 10 
 11 
 12 CString strServer;
 13 CString strObject;
 14 DWORD dwServiceType;
 15 INTERNET_PORT nPort;
 16 AfxParseURL(m_strHttpUrl, dwServiceType, strServer, strObject, nPort);
 17 
 18 
 19 try
 20 {
 21     pHttpConnection = pInternetSession->GetHttpConnection(strServer, nPort);
 22 }
 23 catch (CInternetException* e)
 24 {
 25     CString strError;
 26     strError.Format(_T("GetHttpConnection Error: %u, Context: %u"), e->m_dwError, e->m_dwContext);
 27     AfxMessageBox(strError);
 28 
 29     if (NULL != pHttpConnection)
 30     {
 31         pHttpConnection->Close();
 32         delete pHttpConnection;
 33         pHttpConnection = NULL;
 34     }
 35     if (NULL != pInternetSession)
 36     {
 37         pInternetSession->Close();
 38         delete pInternetSession;
 39         pInternetSession = NULL;
 40     }
 41 
 42     return FALSE;
 43 }
 44 
 45 
 46 strParam.Format(_T("?ccvid=%s&format=%s&time=%I64u"),
 47     m_strVideoid, g_strFormat, currentTime);
 48 strParam.AppendFormat(_T("&hash=%s"), sHash.c_str());
 49 
 50 CString strTempObject = strObject + strParam;
 51 pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, strTempObject);
 52 if (NULL == pHttpFile)
 53 {
 54     AfxMessageBox(_T("OpenRequest Error."));
 55 
 56     if (NULL != pHttpFile)
 57     {
 58         pHttpFile->Close();
 59         delete pHttpFile;
 60         pHttpFile = NULL;
 61     }
 62     if (NULL != pHttpConnection)
 63     {
 64         pHttpConnection->Close();
 65         delete pHttpConnection;
 66         pHttpConnection = NULL;
 67     }
 68     if (NULL != pInternetSession)
 69     {
 70         pInternetSession->Close();
 71         delete pInternetSession;
 72         pInternetSession = NULL;
 73     }
 74 
 75     return FALSE;
 76 }
 77 
 78     
 79 pHttpFile->AddRequestHeaders(_T("Accept: *,*/*"));
 80 pHttpFile->AddRequestHeaders(_T("Accept-Language: zh-cn"));
 81 pHttpFile->AddRequestHeaders(_T("Content-Type: application/x-www-form-urlencoded; charset=utf-8"));
 82 //pHttpFile->AddRequestHeaders(_T("Content-Type: application/json; charset=utf-8"));
 83 pHttpFile->AddRequestHeaders(_T("Accept-Encoding: gzip, deflate"));
 84 strContentRange.Format(, m_startLen, m_endLen - 1, fileLen);
 85 pHttpFile->AddRequestHeaders(_T("Content-Range: bytes %x-%y/%z"));
 86 
 87 
 88 CString strReq;
 89 strReq.Format(_T("courseFullName=%s&courseName=%s&grade=%s&lesson=%s&liveRoomId=%s&section=%s&teacherName=%s&type=%s&recordingTime=%s-%s-%s"),
 90     strVideoName, strTopic, strGrade, strNum, strLiveRoom, strStage, strTeacher, strSubject, strYear, strMonth, strDay);
 91 strStart.Format(_T("--%s\r\nContent-Disposition: form-data;name=\"file%s\";filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n"),
 92     strBOUNDARY, m_strVideoName, m_strVideoName);
 93 strEnd.Format(_T("\r\n--%s--\r\n"),
 94 try
 95 {
 96     pHttpFile->SendRequest(NULL, 0, strReq.GetBuffer(), strReq.GetLength());    //len注意
 97 }
 98 catch (CInternetException* e)
 99 {
100     CString strError;
101     strError.Format(_T("SendRequest Error: %u, Context: %u"), e->m_dwError, e->m_dwContext);
102     AfxMessageBox(strError);
103 
104     if (NULL != pHttpFile)
105     {
106         pHttpFile->Close();
107         delete pHttpFile;
108         pHttpFile = NULL;
109     }
110     if (NULL != pHttpConnection)
111     {
112         pHttpConnection->Close();
113         delete pHttpConnection;
114         pHttpConnection = NULL;
115     }
116     if (NULL != pInternetSession)
117     {
118         pInternetSession->Close();
119         delete pInternetSession;
120         pInternetSession = NULL;
121     }
122 
123     return FALSE;
124 }
125 
126 
127 char szChars[1024 + 1] = { 0 };
128 CString strRawResponse;
129 UINT nReaded = 0;
130 while ((nReaded = pHttpFile->Read((void*)szChars, 1024)) > 0)
131 {
132     szChars[nReaded] = \0;
133     strRawResponse += szChars;
134     memset(szChars, 0, 1024 + 1);
135 }
136 
137 AfxMessageBox(strRawResponse);
138 
139 
140 if (NULL != pHttpFile)
141 {
142     pHttpFile->Close();
143     delete pHttpFile;
144     pHttpFile = NULL;
145 }
146 if (NULL != pHttpConnection)
147 {
148     pHttpConnection->Close();
149     delete pHttpConnection;
150     pHttpConnection = NULL;
151 }
152 if (NULL != pInternetSession)
153 {
154     pInternetSession->Close();
155     delete pInternetSession;
156     pInternetSession = NULL;
157 }
158 
159 
160 return TRUE;

 

Windows MFC HTTP POST请求 函数流程

标签:exce   %x   res   --   length   text   cstring   service   net   

原文地址:https://www.cnblogs.com/diaoss/p/11583744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!