vc++6.0 get web pages via HTTP openurl__c++
Last Update:2018-07-25
Source: Internet
Author: User
Head contains Afxinet.h
2 #include < afxinet.h >
The 3 CInternetSession::OpenURL function prototype is:
4 CStdioFile * OpenURL (LPCTSTR pstrurl,//File URL address
5 DWORD dwcontext = 1,//context ID
6 DWORD dwflags = internet_flag_transfer_ascii,//Mark
7 LPCTSTR pstrheaders = NULL,//Data headers sent to the server
8 DWORD dwheaderslength = 0); Length of data headers sent to the server
9 dwflags can be:
Internet_flag_reload Mandatory Reread data
One internet_flag_dont_cache not saved to the cache
Internet_flag_transfer_ascii using text data
Internet_flag_transfer_binary using binary data
14//Get the Web page method code as follows
CString Geturlcontext (LPCTSTR pstrurl)
16 {
17//Custom HTTP header information
char * headers = "accept:*/*\r\n"
"Accept-language:zh-cn\r\n"
"User-agent:vctestclient\r\n";
CString m_strhtml;
Try
23 {
CInternetSession Sess; Establish a session
CHttpFile * PF = (CHttpFile *) Sess. OpenURL (Pstrurl, 1, internet_flag_transfer_ascii | | Internet_flag_reload,headers,strlen (headers)); Open File
26 Note: The dwflags parameter of the OpenURL method must select the transfer mode for INTERNET_FLAG_TRANSFER_ASCII or internet_flag_transfer_binary
CString Szdata,szalldata;
while (PF-> ReadString (szdata))
29 {
30//Read files
Szalldata + = "\ r \ n";
Szalldata + = Szdata;
33}
PF-> Close ();
Sess. Close ();
m_strhtml = Szalldata;
37}
catch (CException * e)
39 {
AfxMessageBox ("Failed to obtain data");
41}
return m_strhtml;
43}
----------------------------------------------------------------------------------------------------------- -------------------------- Display picture, lpstrimgurl as Picture URL address, hWnd as window handle
HRESULT cbmpdlg::showpic (char * lpstrimgurl,hwnd HWND)
{
HDC hdc_temp =:: GetDC (HWND);
IPicture * PPIC;
IStream * PSTM;
BOOL Bresult;
DWORD Dwfilesize,dwbyteread;
char * headers = "accept:image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, Application/x-shockwave-flash, APPL Ication/vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, Application/xaml+xml, application/ Vnd.ms-xpsdocument, APPLICATION/X-MS-XBAP, application/x-ms-application, */* "
"Accept-encoding:gzip, deflate\r\n."
"Accept-language:zh-cn\r\n"
"Content-type:application/x-www-form-urlencoded\r\n"
"Cache-control:no-cache\r\n";
Reading a picture file on a Web page is actually a chttpfile pointer
CInternetSession session ("HttpClient");
DWORD dwflags = Internet_flag_transfer_binary | Internet_flag_reload | Internet_flag_dont_cache;
CFile * Httpfile = (CFile *) session. OpenURL (Lpstrimgurl, 1, Dwflags,headers,strlen (headers)); 1,dwflags
if (httpfile!= invalid_handle_value)
{
Char achquerybuf[32];
DWORD Dwquerybuflen = sizeof (ACHQUERYBUF);
CHttpFile * PHF = (CHttpFile *) httpfile;
PHF-> queryinfo (Http_query_content_length, Achquerybuf, & Dwquerybuflen, NULL);
dwFileSize = (DWORD) atol (ACHQUERYBUF);
if (dwfilesize = 0xFFFFFFFF)
return E_FAIL;
}
Else
{
return E_FAIL;
}
Allocating Global storage space
Hglobal hglobal = GlobalAlloc (gmem_moveable, dwfilesize);
LPVOID pvData = NULL;
if (hglobal = NULL)
return E_FAIL;
if ((PvData = GlobalLock (hglobal) = = NULL)//Lock allocated memory block
return E_FAIL;
Read the file into the memory buffer
Dwbyteread = Httpfile-> Read (pvdata,dwfilesize);
GlobalUnlock (HGLOBAL);
CreateStreamOnHGlobal (Hglobal, TRUE, & PStm);
Load graphics files
Bresult = OleLoadPicture (Pstm,dwfilesize,true,iid_ipicture, (LPVOID *) & Ppic);
if (FAILED (bresult))
return E_FAIL;
Ole_xsize_himetric Hmwidth; The true width of the picture, in inches
Ole_ysize_himetric Hmheight; The true height of the picture, in inches
Ppic-> get_width (& Hmwidth);
Ppic-> get_height (& Hmheight);
Convert hmwidth and hmheight to pixels distance, 1 inches = 25.4 mm
int nwidth = MulDiv (Hmwidth,getdevicecaps (hdc_temp,logpixelsx), 2540);
int nheight = MulDiv (Hmheight,getdevicecaps (hdc_temp,logpixelsy), 2540);
Output graphics to the screen (a bit like BitBlt)
Bresult = Ppic-> Render (hdc_temp, 0, 0, nwidth,nheight, 0, Hmheight,hmwidth,-hmheight,null);
Ppic-> release ();
Httpfile-> Close (); Close an open file
if (SUCCEEDED (Bresult))
{
return S_OK;
}
Else
{
return E_FAIL;
}
///Display picture, lpstrimgurl as Picture URL address, hWnd as window handle
HRESULT utils::showpic (char * lpstrimgurl,hwnd HWND)
{
HDC hdc_temp = GetDC (hWnd);
IPicture * PPIC;
IStream * PSTM;
BOOL Bresult;
DWORD Dwfilesize,dwbyteread;
Reading a picture file on a Web page is actually a chttpfile pointer
CInternetSession session ("HttpClient");
CFile * Httpfile = (CFile *) session. OpenURL (Lpstrimgurl);
if (httpfile!= invalid_handle_value)
{
dwFileSize = Httpfile-> getlength (); Get the number of file bytes
if (dwfilesize = 0xFFFFFFFF)
return E_FAIL;
}
Else
{
return E_FAIL;
}
Allocating Global storage space
Hglobal hglobal = GlobalAlloc (gmem_moveable, dwfilesize);
LPVOID pvData = NULL;
if (hglobal = NULL)
return E_FAIL;
if ((PvData = GlobalLock (hglobal) = = NULL)//Lock allocated memory block
return E_FAIL;
Read the file into the memory buffer
Dwbyteread = Httpfile