Using CInternetFile open an Url
Here is code:
// TODO: Add your control notification handler code here CInternetSession InetSession; CString strLine; CInternetFile *pInetFile = NULL; try { pInetFile = (CInternetFile *)InetSession.OpenURL(_T("https://account.cnblogs.com/signin?returnUrl=https%3A%2F%2Fwww.cnblogs.com%2F")); } catch (CInternetException *pException) { pInetFile = NULL; pException->Delete(); } char sRecived[1024] = { 0 }; if (pInetFile) { while (pInetFile->ReadString((LPTSTR)sRecived, 1024)) { m_Edit2_context+=(CString)sRecived; } UpdateData(false); }
there would be a bug which will get an unknown context from Url.
Another way to get the Url sourse:
unsigned short nPort; //用于保存目标HTTP服务端口 CString strServer, strObject; //strServer用于保存服务器地址,strObject用于保存文件对象名称 DWORD dwServiceType; //dwServiceType用于保存服务类型,dwRet用于保存提交GET请求返回的状态号 UpdateData(true); setlocale(LC_CTYPE, "chs"); //解析URL,获取信息 if (!AfxParseURL(m_Edit1_link, dwServiceType, strServer, strObject, nPort)) { return; } //建立网络连接 if (1) { CInternetSession *session = new CInternetSession(); CHttpFile *pHtFile = NULL; CHttpConnection *pHtCon = session->GetHttpConnection(strServer, nPort,"admin","12345"); pHtFile = pHtCon->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject); pHtFile->SendRequest(); if (pHtFile != false) { CString sRecived; while (pHtFile->ReadString(sRecived)!=NULL) { m_Edit2_context += sRecived; } UpdateData(false); } session->Close(); delete pHtCon; pHtCon = NULL; delete pHtFile; pHtFile = NULL; }
Now we list the last way by using vc++
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// m_WinInetDlg.cpp : implementation file // #include "stdafx.h" #include "m_WinInet.h" #include "m_WinInetDlg.h" #include <afxinet.h> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CM_WinInetDlg dialog CM_WinInetDlg::CM_WinInetDlg(CWnd* pParent /*=NULL*/) : CDialog(CM_WinInetDlg::IDD, pParent) { //{{AFX_DATA_INIT(CM_WinInetDlg) m_Edit2 = _T(""); m_Edit1 = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CM_WinInetDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CM_WinInetDlg) DDX_Text(pDX, IDC_EDIT2, m_Edit2); DDX_Text(pDX, IDC_EDIT1, m_Edit1); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CM_WinInetDlg, CDialog) //{{AFX_MSG_MAP(CM_WinInetDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, OnButton1) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CM_WinInetDlg message handlers BOOL CM_WinInetDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } void CM_WinInetDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CM_WinInetDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CM_WinInetDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CM_WinInetDlg::OnButton1() { // TODO: Add your control notification handler code here const MAXBUF = 4096; CInternetSession session; CHttpConnection* pConnection = NULL; CHttpFile* pFile = NULL; char* buffer = new char[MAXBUF]; UINT nBytesRead = 0; CString strServer, strObject; unsigned short nPort; DWORD dwServiceType; UpdateData(TRUE); if(m_Edit1.IsEmpty()) return; if(!AfxParseURL(m_Edit1, dwServiceType, strServer, strObject, nPort)) { MessageBox("HTTP请求解析失败,请重新输入!"); return; } try { pConnection = session.GetHttpConnection((LPCTSTR)strServer, nPort); pFile = pConnection->OpenRequest(1, strObject); pFile->SendRequest(); nBytesRead = pFile->Read(buffer, MAXBUF - 1); buffer[nBytesRead] = '\0'; m_Edit2 = buffer; m_Edit2+="\n\r...结束啦..."; UpdateData(FALSE); } catch(CInternetException* e) { TCHAR szError[1024]; e->GetErrorMessage(szError,1024); ::AfxMessageBox(szError); e->Delete(); } if(pFile) delete pFile; if(pConnection) delete pConnection; delete [] buffer; return; }
End.