我这里有一段程序,用来在一个对话框里显示出一次http request的原始信息,不过使用Inet API做的

    技术2026-04-09  1

    我这里有一段程序,用来在一个对话框里显示出一次http     request的原始信息,不过使用Inet     API做的,希望能有帮助。           void     CHTTPRequestDlg::OnButtonRequest()         {       UpdateData(TRUE);       HINTERNET     hInternet     =     InternetOpen("Mozilla/4.0     (compatible;     MSIE     5.0;     Windows     NT     5.0)",     INTERNET_OPEN_TYPE_DIRECT,       NULL,     NULL,     NULL);       HINTERNET     hSession     =     InternetConnect(hInternet,     m_strHost,       m_nPort,     "username",     "password",       INTERNET_SERVICE_HTTP,     0,     0);       char*     szAccept[]     =     {"*/*",     NULL};       CString     strVerb;       m_comboVerb.GetWindowText(strVerb);       HINTERNET     hRequest     =     HttpOpenRequest(hSession,     strVerb,     m_strObject,     NULL,     NULL,     (LPCSTR*)szAccept,     0,     0);       struct       {       char*     Language;       char*     Encoding;       char*     ContentType;       }Headers     =     {"Accept-Language:     zh-cn/r/n",                       "Accept-Encoding:     gzip,     deflate/r/n",       "Content-Type:     application/x-www-form-urlencoded/r/n"};           if(m_bLanguage)       HttpAddRequestHeaders(hRequest,     Headers.Language,           -1,     HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);       if(m_bEncoding)       HttpAddRequestHeaders(hRequest,     Headers.Encoding,           -1,     HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);       if(m_bContentType)       HttpAddRequestHeaders(hRequest,     Headers.ContentType,     -1,     HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);       LPCSTR     lpAddHeader     =     NULL,     lpContent     =     NULL;       if(m_strHeaders.GetLength())       {       if(m_strHeaders.Right(2)     !=     "/r/n")       m_strHeaders     +=     "/r/n";       lpAddHeader     =     (LPCSTR)m_strHeaders;       }       if(m_strContent.GetLength()     &&     (strVerb     ==     "POST"     ||     strVerb     ==     "PUT"))       lpContent     =     (LPCSTR)m_strContent;       HttpSendRequest(hRequest,     lpAddHeader,     -1,     (LPVOID)lpContent,     m_strContent.GetLength());           m_editContentGot.SetSel(0,     -1);       m_editContentGot.ReplaceSel("");               LPSTR             lpszData; //     buffer     for     the     data       DWORD             dwSize;                                 //     size     of     the     data     available       DWORD             dwDownloaded; //     size     of     the     downloaded     data           //     Set     the     cursor     to     an     hourglass.       SetCursor(LoadCursor(NULL,IDC_WAIT));           //     This     loop     handles     reading     the     data.           while(1)       {       //     The     call     to     InternetQueryDataAvailable     determines     the     amount     of         //     data     available     to     download.       if     (!InternetQueryDataAvailable(hRequest,&dwSize,0,0))       {       SetCursor(LoadCursor(NULL,IDC_ARROW));       break;       }       else       {                 //     Allocates     a     buffer     of     the     size     returned     by     InternetQueryDataAvailable       lpszData     =     new     char[dwSize+1];           //     Reads     the     data     from     the     HINTERNET     handle.       if(!InternetReadFile(hRequest,(LPVOID)lpszData,dwSize,&dwDownloaded))       {       delete[]     lpszData;       break;       }       else       {       //     Adds     a     null     terminator     to     the     end     of     the     data     buffer       lpszData[dwDownloaded]='/0';           int     nLen     =     m_editContentGot.GetWindowTextLength();       m_editContentGot.SetSel(nLen-1,     nLen-1);       m_editContentGot.ReplaceSel(lpszData);           //     Delete     the     two     buffers       delete[]     lpszData;           //     Check     the     size     of     the     remaining     data.       If     it     is     zero,     break.       if     (dwDownloaded     ==     0)       break;       }       }       }           //     Close     the     HINTERNET     handle       InternetCloseHandle(hRequest);       InternetCloseHandle(hSession);       InternetCloseHandle(hInternet);           //     Set     the     cursor     back     to     an     arrow       SetCursor(LoadCursor(NULL,IDC_ARROW));       }   

    最新回复(0)