一个简单的HTTP通讯的例子,使用了CInternetSession,CHttpConnection,CHttpFile三个类。

void CBaseHttpDlg::OnBnClickedGet() { // TODO: 在此添加控件通知处理程序代码 UpdateData(); CString txt; m_RichEditHttpRespone.GetWindowText(txt); CInternetSession InterSession; CHttpConnection* con = InterSession.GetHttpConnection(m_strURL); CHttpFile* pf = con->OpenRequest(con->HTTP_VERB_GET, m_strURL); pf->AddRequestHeaders(_T("Content-Type: application/x-www-form-urlencoded")); pf->AddRequestHeaders(_T("Accept: */*")); pf->SendRequest(); DWORD dwStatus = 0; DWORD dwBuffLen = sizeof(dwStatus); BOOL bSuccess = pf->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen); CString s; s.Format(_T("/r/nQueryInfo : %d"), dwStatus); txt += s; if( bSuccess && dwStatus>= 200 && dwStatus<300) { char buffer[10240]; memset(buffer, 0, 10240); int nReadCount = 0; txt += "/r/nContent : "; while((nReadCount = pf->Read(buffer, 10240)) > 0) { txt += buffer; memset(buffer, 0, 10240); } } else { s.Format(_T("/r/nCode : %d"), bSuccess); txt += s + _T("/r/n网站服务器错误 ") + m_strURL; } txt += "/r/nGetObject : "; txt += pf->GetObject(); txt += "/r/n"; m_RichEditHttpRespone.SetWindowText(txt); UpdateData(FALSE); pf->Close(); InterSession.Close(); }

 

这只是一个最基本的东西,还需要做很多额外的工作,待补充。

你可能感兴趣的:(C++)