Windows MFC HTTP POST请求 函数流程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Windows MFC HTTP POST请求 函数流程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4526字,纯文字阅读大概需要7分钟。
内容图文
![Windows MFC HTTP POST请求 函数流程](/upload/InfoBanner/zyjiaocheng/952/5278b7068ea3407eb5e834cf55b1e8d2.jpg)
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§ion=%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请求 函数流程全部内容,希望文章能够帮你解决Windows MFC HTTP POST请求 函数流程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。