#include <Windows.h> #include <atlstr.h> //可以用 cstring了 哈哈哈哈 #include <tlhelp32.h> #include <stdio.h> #include <locale> //#pragma comment(lib,"kernel32.lib") //#pragma comment(lib,"user32.lib") //#pragma comment(linker, "/SUBSYSTEM:windows") //#pragma comment(linker, "/ENTRY:main") #import <msxml3.dll> byte * XmlHttp(LPWSTR szHttpAddr,PDWORD pdwLen=0); //返回 byte* 必须 外部 free() byte * XmlHttp(LPWSTR szHttpAddr,PDWORD pdwLen) //返回 byte* 必须 外部 free() { CoInitialize(0); MSXML2::IXMLHTTPRequestPtr http=NULL;//(__uuidof(XMLHTTP)); HRESULT hr; hr=http.CreateInstance(L"Msxml2.XMLHTTP"); if (hr!=0) return NULL; hr=http->open(L"GET", szHttpAddr, false); if (hr!=0) { if (pdwLen!=NULL) *pdwLen=0; http.Release(); CoUninitialize(); return NULL; } // http->setRequestHeader(L"Referer",""); // http->setRequestHeader(L"Accept-Language","zh-cn"); // http->setRequestHeader(L"Content-Type","application/x-www-form-urlencoded"); //下面这个协议头貌似很好用 http->setRequestHeader(L"Connection",L"keep-alive"); http->setRequestHeader(L"Accept",L"application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); http->setRequestHeader(L"User-Agent",L"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10"); // http->setRequestHeader(L"Accept-Encoding",L"gzip,deflate,sdch"); http->setRequestHeader(L"Accept-Language",L"zh-CN,zh;q=0.8"); hr=http->send(); if (hr!=0) { if (pdwLen!=NULL) *pdwLen=0; http.Release(); CoUninitialize(); return NULL; } _variant_t vbin = http-> responseBody; char * pBuf2=NULL; SafeArrayAccessData(vbin.parray,(void **)&pBuf2); if (pBuf2==NULL) { if (pdwLen!=NULL) *pdwLen=0; SafeArrayUnaccessData(vbin.parray); http.Release(); CoUninitialize(); return NULL; } long size=vbin.parray-> rgsabound[0].cElements; byte *bRet=(byte*)malloc(size+2); printf("len:%d %X",size,bRet); bRet[size]=bRet[size+1]=0; memcpy(bRet,pBuf2,size); SafeArrayUnaccessData(vbin.parray); http.Release(); CoUninitialize(); if (pdwLen!=NULL) *pdwLen=size; return bRet;//外部自己必须 Free; } int main() { setlocale(LC_CTYPE, "");//让wprintf 支持中文 byte *bSREt=XmlHttp(L"http://3g.163.com"); printf("/n%x/n",bSREt); int nNeedLen=MultiByteToWideChar(CP_UTF8,0,(LPCSTR)bSREt,-1,NULL,NULL); printf("needLen:%d/n",nNeedLen); WCHAR *szS=(WCHAR*)malloc(nNeedLen*sizeof(WCHAR)+4); MultiByteToWideChar(CP_UTF8,0,(LPCSTR)bSREt,-1,szS,nNeedLen+2);//原来转换网页utf8如此简单 printf("ret/n"); wprintf(L"%s/n",szS); free(bSREt); free(szS); system("pause"); return 0; }