C + + Get URL picture, HTML file, CInternetSession "Go"

Source: Internet
Author: User
Tags int size

http://blog.csdn.net/gnixuyil/article/details/7688439

Get Network pictures

[CPP]View PlainCopy
  1. CString url="Http://www.google.com.hk/images/srpr/logo3w.png"
  2. CInternetSession session;
  3. CHttpFile *httpfile = (CHttpFile *) session. OpenURL (URL);
  4. CStdioFile Imgfile;
  5. Char buff[1024]; //Cache
  6. Imgfile.open ("picture name. png", Cfile::modecreate | Cfile::modewrite |  Cfile::typebinary);
  7. DWORD Dwstatuscode;
  8. Httpfile->queryinfostatuscode (Dwstatuscode);
  9. if (Dwstatuscode = = HTTP_STATUS_OK) {
  10. int size=0;
  11. Do {
  12. Size = Httpfile->read (buff,1024); //Read pictures
  13. Imgfile.write (buff,size);
  14. }while (Size > 0);
  15. }
  16. Httpfile->close ();
  17. Session. Close ();


Gets the HTML of the URL

[CPP]View PlainCopy
  1. CInternetSession session;
  2. CHttpFile *httpfile = (CHttpFile *) session. OpenURL (M_url);
  3. DWORD Dwstatuscode;
  4. Httpfile->queryinfostatuscode (Dwstatuscode);
  5. CString getdata=_t ("");
  6. if (Dwstatuscode = = HTTP_STATUS_OK) {
  7. CString line_data=_t ("");
  8. While (Httpfile->readstring (line_data)) {
  9. GetData + = Line_data; //Read HTML
  10. }
  11. GetData. TrimRight ();
  12. }
  13. Httpfile->close (); //HTML data has been placed in the GetData
  14. Session. Close ();
  15. If the Utf_8 page is saved in GetData (you can see the Meta field of HTML)
  16. Strcoding CFM; //Code conversion class, see below for details
  17. String temp = (LPCSTR) GetData.  GetBuffer (); //Web data, converted to string type
  18. string output;
  19. Utf_8 to GB2312 so that MFC controls can display
  20. cfm.  utf_8togb2312 (output, (char *) temp.data (), strlen (Temp.data ()));
  21. If the MFC character set is Unicode, you also need to convert multibyte to wide-byte
  22. temp = output;
  23. DWORD dwnum = MultiByteToWideChar (CP_ACP, 0, Temp.c_str (),-1, NULL, 0);
  24. wchar_t *pwtext;
  25. Pwtext = new wchar_t[dwnum];
  26. MultiByteToWideChar (CP_ACP, 0, Temp.c_str (),-1, Pwtext, dwnum);
  27. Gets the converted result m_data used to display
  28. m_data = Pwtext;
  29. delete []pwtext;


Encoding Conversion class: http://blog.csdn.net/gnixuyil/article/details/7688469

C + + Get URL picture, HTML file, CInternetSession "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.