C++ 获取URL内容的实例
我就废话不多说了,大家还是直接看代码吧~
以下内容摘自StackOverFlow链接
#ifndefHTTPUTIL_H #defineHTTPUTIL_H #include#include #include usingstd::string; #pragmacomment(lib,"ws2_32.lib") voidmParseUrl(char*mUrl,string&serverName,string&filepath,string&filename); SOCKETconnectToServer(char*szServerName,WORDportNum); intgetHeaderLength(char*content); char*readUrl2(char*szUrl,long&bytesReturnedOut,char**headerOut); char*sendRequest(charszUrl[]){ WSADATAwsaData; //charszUrl[]="http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp"; longfileSize; char*memBuffer,*headerBuffer; memBuffer=headerBuffer=nullptr; if(WSAStartup(0x101,&wsaData)!=0) returnnullptr; memBuffer=readUrl2(szUrl,fileSize,&headerBuffer); printf("returnedfromreadUrl\n"); printf("datareturned:\n%s",memBuffer); if(fileSize!=0){ //delete(memBuffer); delete(headerBuffer); } WSACleanup(); returnmemBuffer; } voidmParseUrl(char*mUrl,string&serverName,string&filepath,string&filename){ string::size_typen; stringurl=mUrl; if(url.substr(0,7)=="http://") url.erase(0,7); if(url.substr(0,8)=="https://") url.erase(0,8); n=url.find('/'); if(n!=string::npos){ serverName=url.substr(0,n); filepath=url.substr(n); n=filepath.rfind('/'); filename=filepath.substr(n+1); } else{ serverName=url; filepath="/"; filename=""; } } SOCKETconnectToServer(char*szServerName,WORDportNum){ structhostent*hp; unsignedintaddr; structsockaddr_inserver; SOCKETconn; conn=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(conn==INVALID_SOCKET) returnNULL; if(inet_addr(szServerName)==INADDR_NONE){ hp=gethostbyname(szServerName); }else{ addr=inet_addr(szServerName); hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET); } if(hp==nullptr){ closesocket(conn); returnNULL; } server.sin_addr.s_addr=*((unsignedlong*)hp->h_addr); server.sin_family=AF_INET; server.sin_port=htons(portNum); if(connect(conn,(structsockaddr*)&server,sizeof(server))){ closesocket(conn); returnNULL; } returnconn; } intgetHeaderLength(char*content){ constchar*srchStr1="\r\n\r\n",*srchStr2="\n\r\n\r"; char*findPos; intofset=-1; findPos=strstr(content,srchStr1); if(findPos!=nullptr){ ofset=findPos-content; ofset+=strlen(srchStr1); } else{ findPos=strstr(content,srchStr2); if(findPos!=nullptr){ ofset=findPos-content; ofset+=strlen(srchStr2); } } returnofset; } char*readUrl2(char*szUrl,long&bytesReturnedOut,char**headerOut){ constintbufSize=512; charreadBuffer[bufSize],sendBuffer[bufSize],tmpBuffer[bufSize]; char*tmpResult=nullptr,*result; SOCKETconn; stringserver,filepath,filename; longtotalBytesRead,thisReadSize,headerLen; mParseUrl(szUrl,server,filepath,filename); /step1,connect// conn=connectToServer((char*)server.c_str(),80); /step2,sendGETrequest/ sprintf(tmpBuffer,"GET%sHTTP/1.0",filepath.c_str()); strcpy(sendBuffer,tmpBuffer); strcat(sendBuffer,"\r\n"); sprintf(tmpBuffer,"Host:%s",server.c_str()); strcat(sendBuffer,tmpBuffer); strcat(sendBuffer,"\r\n"); strcat(sendBuffer,"\r\n"); send(conn,sendBuffer,strlen(sendBuffer),0); //SetWindowText(edit3Hwnd,sendBuffer); printf("Bufferbeingsent:\n%s",sendBuffer); /step3-getreceivedbytes //Receiveuntilthepeerclosestheconnection totalBytesRead=0; while(1){ memset(readBuffer,0,bufSize); thisReadSize=recv(conn,readBuffer,bufSize,0); if(thisReadSize<=0) break; tmpResult=(char*)realloc(tmpResult,thisReadSize+totalBytesRead); memcpy(tmpResult+totalBytesRead,readBuffer,thisReadSize); totalBytesRead+=thisReadSize; } headerLen=getHeaderLength(tmpResult); longcontenLen=totalBytesRead-headerLen; result=newchar[contenLen+1]; memcpy(result,tmpResult+headerLen,contenLen); result[contenLen]=0x0; char*myTmp; myTmp=newchar[headerLen+1]; strncpy(myTmp,tmpResult,headerLen); myTmp[headerLen]=NULL; delete(tmpResult); *headerOut=myTmp; bytesReturnedOut=contenLen; closesocket(conn); return(result); } #endif//HTTPUTIL_H
测试代码:
#include#include #include"HttpUtil.h" #include usingstd::string; usingnamespacestd; intmain(){ char*resData=sendRequest("http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp"); stringstr=resData; cout< 补充知识:C++处理URL的方法,项目有用到,过程记录如下
由于这块需要转换成unicode码,也就是将字符串传换成unicode码,因此需要对输入的字符串做处理,同时又分两种情况,中文非中文的处理,要区别对待,首先要对输入的字符串进行识别:
int是不是中文(char*str) { charch; while(1) { ch=*str++; if(ch==0) { break; } if(ch&0x80) { if(*str&0x80) { returntrue; } } else { returnfalse; } } return0; }然后要进行相应转换
//--------------------------------------------------------------------- //函数:W2C //功能:将16位wchar_t转换为8位char[2] //参数:w_cn为待转换的16位字符,c_cn[]为转换后的8位字符 //备注:wchar_t的高位字节应该存储在char数组的低位字节 //作者:xxxx //--------------------------------------------------------------------- voidW2C(wchar_tw_cn,charc_cn[]){c_cn[0]=w_cn>>8;c_cn[1]=(char)w_cn;}然后主体转换代码:
//------------------------------------------------------------------- //函数:ToHex //功能:将16位字符串转换为十六进制字符串 //参数:待转换的字符串,字符串长度 //返回值:转换后的字符串 //作者:xxxx //------------------------------------------------------------------- CStringToHex(CStringData,longnDataLength) { CStringsResult; for(longnLoop=0;nLoop>4)&0x0f]; unsignedcharchHexB=hex[(unsignedchar)(c_cn[i])&0x0f]; sResult+=(char)chHexA; sResult+=(char)chHexB; } } else { sResult+=ch; } } returnsResult; } 到这里基本上结束了~也查看了一些资料,虽说这个功能简单,但是过程有点曲折。但总算完成了,留个纪念吧~希望对其他人有帮助~希望大家多多支持毛票票。