zoukankan      html  css  js  c++  java
  • Windows MFC HTTP POST请求 函数流程

    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
    Content-Disposition: form-data;name="file%s";filename="%s"
    Content-Type: application/octet-stream
    
    "),
     92     strBOUNDARY, m_strVideoName, m_strVideoName);
     93 strEnd.Format(_T("
    --%s--
    "),
     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] = '';
    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;
  • 相关阅读:
    页面反向映射之文件页面
    页面反向映射之匿名页面
    cp so文件导致进程SIGBUS或者SEGV原因分析
    由systemtap直接修改内核代码段想到的
    epoll的内核实现
    从一些现象看printf的缓冲机制
    Linux由浅入深学习 (转)
    每天一个Linux命令 (转)
    Redis与数据库同步问题
    PHP使用文件流下载文件方法(附:解决下载文件内容乱码问题)
  • 原文地址:https://www.cnblogs.com/diaoss/p/11583744.html
Copyright © 2011-2022 走看看