Windows MFC HTTP GET请求 函数流程
Posted diaoss
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows MFC HTTP GET请求 函数流程相关的知识,希望对你有一定的参考价值。
Windows MFC HTTP GET请求 函数流程
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 pInternetSession = new CInternetSession(AfxGetAppName()); 9 10 11 CString strServer; 12 CString strObject; 13 DWORD dwServiceType; 14 INTERNET_PORT nPort; 15 AfxParseURL(m_strHttpUrl, dwServiceType, strServer, strObject, nPort); 16 17 18 try 19 20 pHttpConnection = pInternetSession->GetHttpConnection(strServer, nPort); 21 22 catch (CInternetException* e) 23 24 CString strError; 25 strError.Format(_T("GetHttpConnection Error: %u, Context: %u"), e->m_dwError, e->m_dwContext); 26 AfxMessageBox(strError); 27 28 if (NULL != pHttpConnection) 29 30 pHttpConnection->Close(); 31 delete pHttpConnection; 32 pHttpConnection = NULL; 33 34 if (NULL != pInternetSession) 35 36 pInternetSession->Close(); 37 delete pInternetSession; 38 pInternetSession = NULL; 39 40 41 return FALSE; 42 43 44 45 strParam.Format(_T("?ccvid=%s&filename=%s&filesize=%u&first=1&format=%s&md5=%s&servicetype=%s&uid=%s&time=%I64u"), 46 m_strVideoid, m_strVideoName, m_fileLen, g_strFormat, m_strFileMD5, m_strServicetype, m_strUserid, currentTime); 47 strParam.AppendFormat(_T("&hash=%s"), sHash.c_str()); 48 49 CString strTempObject = strObject + strParam; 50 pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strTempObject); 51 if (NULL == pHttpFile) 52 53 AfxMessageBox(_T("OpenRequest Error.")); 54 55 if (NULL != pHttpFile) 56 57 pHttpFile->Close(); 58 delete pHttpFile; 59 pHttpFile = NULL; 60 61 if (NULL != pHttpConnection) 62 63 pHttpConnection->Close(); 64 delete pHttpConnection; 65 pHttpConnection = NULL; 66 67 if (NULL != pInternetSession) 68 69 pInternetSession->Close(); 70 delete pInternetSession; 71 pInternetSession = NULL; 72 73 74 return FALSE; 75 76 77 78 pHttpFile->AddRequestHeaders(_T("Content-Type: application/x-www-form-urlencoded; charset=utf-8")); 79 80 81 try 82 83 pHttpFile->SendRequest(); 84 85 catch (CInternetException* e) 86 87 CString strError; 88 strError.Format(_T("SendRequest Error: %u, Context: %u"), e->m_dwError, e->m_dwContext); 89 AfxMessageBox(strError); 90 91 if (NULL != pHttpFile) 92 93 pHttpFile->Close(); 94 delete pHttpFile; 95 pHttpFile = NULL; 96 97 if (NULL != pHttpConnection) 98 99 pHttpConnection->Close(); 100 delete pHttpConnection; 101 pHttpConnection = NULL; 102 103 if (NULL != pInternetSession) 104 105 pInternetSession->Close(); 106 delete pInternetSession; 107 pInternetSession = NULL; 108 109 110 return FALSE; 111 112 113 114 char szChars[1024 + 1] = 0 ; 115 CString strRawResponse; 116 UINT nReaded = 0; 117 while ((nReaded = pHttpFile->Read((void*)szChars, 1024)) > 0) 118 119 szChars[nReaded] = ‘\0‘; 120 strRawResponse += szChars; 121 memset(szChars, 0, 1024 + 1); 122 123 124 AfxMessageBox(strRawResponse); 125 126 127 if (NULL != pHttpFile) 128 129 pHttpFile->Close(); 130 delete pHttpFile; 131 pHttpFile = NULL; 132 133 if (NULL != pHttpConnection) 134 135 pHttpConnection->Close(); 136 delete pHttpConnection; 137 pHttpConnection = NULL; 138 139 if (NULL != pInternetSession) 140 141 pInternetSession->Close(); 142 delete pInternetSession; 143 pInternetSession = NULL; 144 145 146 return TRUE;
以上是关于Windows MFC HTTP GET请求 函数流程的主要内容,如果未能解决你的问题,请参考以下文章
如何捕获从 MFC Web 浏览器 C++ 发出的 GET/POST 请求