Home > Software engineering >  Beginners MFC, how to use a website for pictures or data and display?
Beginners MFC, how to use a website for pictures or data and display?

Time:09-27

Just contact with MFC, the feeling is code readability is not high, want to write a MFC procedures, through the button to get websites such as http://baidu.com and display images, baidu search for a long time, feeling is very complex, is there any library or function can be called directly?
CInternetSession mysession;
CHttpConnection * myconn=mysession. GetHttpConnection (L "http://www.baidu.com");
CHttpFile * myfile=myconn - & gt; OpenRequest (L "GET", "L/index. HTML");
Myfile - & gt; The SendRequest ();
Cstrings mystr;
Cstrings TMP.
While (myfile - & gt; ReadString (TMP))
{
Mystr +=TMP;
}
Myfile - & gt; Close ();
Myconn - & gt; Close ();
Mysession. Close ();
The delete myfile.
The delete myconn;
Myfile=0;
Myconn=0;

CodePudding user response:

Hope to have the source code or written procedures for reference, thank you

CodePudding user response:

Baidu search "curl"

CodePudding user response:

Call wininet API download images related to the local or memory
And then load the displayed.

Baidu relevant knowledge respectively, apart to simple.

To find how to use wininet API to download online picture
And then to find how to load the local pictures showed.

CodePudding user response:

MFC didn't so much available
Many functions are their own design

CodePudding user response:

UrlDownloadToFile Trojan's favourite, download a word

Then WinExec execution,

You should be displayed the MFC, is to use gdi +

CodePudding user response:

The first step: HTTP requests for the returned HTML page code, this estimate is ready, you
Step 2: parse the returned HTML code, extract & lt; Img> Labels of the SRC attribute, the attribute value is the URL of the picture, if the URL is an absolute path, again to launch the HTTP request to get the picture, returns the image of the original data, you have pictures directly into the file, if it is a relative path, which is not at the beginning of the URL is HTTP, this time you have to observe the HTML source code, the main directory are extracted from the URL, then add picture URL is the absolute path to the URL

About the web & lt; Img> Extraction and its SRC attribute labels, you can write code to extract, given you may beginner, you can use the ready-made HTML parsing library, here recommend Google gumbo - parser
For the library enumerated all & lt; Img> Label and obtain the SRC attribute values, the library can be download in the making,

CodePudding user response:

Thank you for your reply! I use the code below to obtain data why messagebox display is garbled?
Cstrings HttpGet (cstrings strUrl)
{
Cstrings strResult;
CInternetSession session (NULL, 0);
CHttpFile * pHTTPFile=NULL;

If (pHTTPFile=(CHttpFile *) session. OpenURL (strUrl))
{
Cstrings strText;
While (pHTTPFile - & gt; ReadString (strText))
StrResult=strText + + _T (" \ r \ n ");
PHTTPFile - & gt; Close ();
The delete pHTTPFile;
}
Return strResult;
}

CodePudding user response:



 
# include "resource. H"
# include "stdafx. H"
#include
#include

# pragma comment (lib, "wininet. Lib")
# pragma comment (lib, "kernel32. Lib")
Void Entry ();
Int APIENTRY WinMain (HINSTANCE HINSTANCE,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
Int nCmdShow)
{

Int ret=URLDownloadToFile (NULL, "http://www.google.cn/intl/zh-CN/images/logo_cn.gif",
"C: \ \ logo.gif", 0, NULL);


If (ret==S_OK)//if download success
{
Okok MessageBox (NULL, ", "" OK", MB_OK);
}

Entry ();
return 0;
}


Void Entry ()
{
HINTERNET hSession=InternetOpen (NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0).
HINTERNET hConnection=InternetOpenUrl (hSession, "http://www.google.com", NULL, 0, 0).

BYTE Buffer [4096].
DWORD Read=0, I;
HANDLE hFile=CreateFile (" c: \ \ index. HTM, "GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
InternetReadFile (hConnection, Buffer, sizeof (Buffer), & amp; Read);
While (Read!=0)
{
WriteFile (hFile, Buffer, Read, & amp; I, NULL);
InternetReadFile (hConnection, Buffer, sizeof (Buffer), & amp; Read);
}
The CloseHandle (hFile);

InternetCloseHandle (hConnection);
InternetCloseHandle(hSession);
}


Two ways to download anything,

CodePudding user response:

Now most web pages are UTF8 encoding, so you download the HTML string is UTF8 encoding, MessageBox appeared only ANSI and Unicode, such as to display the correct string please transcoding, give you a transcoding code:
 LPCSTR UIAsset: : UTF8ToANSI (LPCSTR utf8) 
{
Int nLen=: : MultiByteToWideChar (CP_UTF8, NULL, utf8, strlen (utf8), NULL, 0).
Would be * ucs=new would [nLen + 1];
: : MultiByteToWideChar (CP_UTF8, NULL, utf8, strlen (utf8), ucs, nLen);
Ucs [nLen]='\ 0';
NLen=: : WideCharToMultiByte (CP_ACP, NULL, ucs, wcslen (ucs), NULL, 0, NULL, NULL);
Char * ANSI=new char [nLen + 1];
: : WideCharToMultiByte (CP_ACP, NULL, ucs, wcslen (ucs), ANSI, nLen, NULL, NULL);
The ANSI/nLen='\ 0';
The delete [] ucs;
Return the ANSI;
}

LPCWSTR UTF8ToUCS (LPCSTR utf8)
{
Int nLen=: : MultiByteToWideChar (CP_UTF8, NULL, utf8, strlen (utf8), NULL, 0).
Would be * ucs=new would [nLen + 1];
: : MultiByteToWideChar (CP_UTF8, NULL, utf8, strlen (utf8), ucs, nLen);
Ucs [nLen]='\ 0';
Return the ucs.
}

CodePudding user response:

CodePudding user response:

I want to post data to a url and get the results of his return, what do you have available code??????? Added to the 100 points can also! Thank you all!!! nullnullnullnullnullnullnull
  • Related