MFC C + + gets extranet IP address

Source: Internet
Author: User

#include <afxinet.h>//gb2312 converted to unicodewchar_t* gb2312tounicode (const char* szgbstring) {UINT ncodepage = 936;    /gb2312 int Nlength=multibytetowidechar (ncodepage,0,szgbstring,-1,null,0);    wchar_t* pbuffer = new Wchar_t[nlength+1];    MultiByteToWideChar (ncodepage,0,szgbstring,-1,pbuffer,nlength);    pbuffer[nlength]=0; return pbuffer;} CString Cnetdlg::getnetip () {    cstring internetip = _t ("");    char chSource[4096] = {0} ;    cstring straddress;    cinternetsession mysession (NULL,0);     chttpfile* myhttpfile=null;    straddress = _t ("http://20140507.ip138.com/ic.asp");//ip138 Web page      TRY    {        myhttpfile = (chttpfile*) mysession.openurl (straddress);//Read network address         while (Myhttpfile->read (Chsource, 4096))         {         &nbsp  //Loop read downloaded page text             int begin = 0;     & nbsp      //now only deal with the case of character set gb2312             begin = CKMP <byte*>::find (byte*) Chsource, strlen (Chsource), (byte*) _c ("charset=gb2312"),;                         if (begin! =-1)    & nbsp;        {                wchar* Wchsource = Gb2312tounicode (chsource);                 internetip = wchsource;                 Delete[] wchsource;            }             begin = Internetip.find (_t ("["), 0);           if (begin!=-1)//If you find "[", find "]"   the text inside the brackets is your extranet ip    & nbsp;       {                 int End=internetip.find (_t ("]"));                 Internetip = Internetip.mid (begin+1, end-begin-1);//extract extranet ip                 break;            }        }        myhttpfile->close ();         Mysession.close ();    }    catch (cinternetexception,e)     {         internetip.format (t_t (_$_string_format_1), E->m_dwerror, E->m_dwcontext);     }    catch_all (e)     {        tchar Ch[max_path] = {0};        e->geterrormessage (CH, MAX_PATH);         internetip.format (t_t (_$_string_format_1), -3, ch);    }    END_CATCH_ALL    trace (internetip);    return Internetip;}


References:

http://blog.csdn.net/cbk861110/article/details/7844729


Web address

Http://20140507.ip138.com/ic.asp

From

Http://www.ip138.com

Found in the Web page source code.

<TR><TD align= "center" >

MFC C + + gets extranet IP address

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.