这是个html页面,网站:http://www.ip138.com:8080/search.asp 
在VC中我如何编写代码获取返回的信息,例如在对话框模式下,一个Edit,一个Button,在一个Edit中输入手机号码,点击Button后要求返回查询手机号码的信息! 
这下面的该论坛的一个达人写的一个方法: 
void CCMDlg::Post(CString &csResponse,const char *szServer,INTERNET_PORT &nPort,const char *szObject,const char *szData,DWORD &dwHttpStatus,BOOL bAutoRedirect) 

//参数说明: 
/* 
csResponse:返回的数据 
szServer:服务器 
nPort:端口 
szObject:URI 
szData:正文内容 
dwHttpStatus:状态码 
bAutoRedirect:是否自动转向 
*/ 
CInternetSession *pSession=NULL; 
CHttpConnection *pConnection=NULL; 
CHttpFile *pHttpFile=NULL; try{ 
pSession=new CInternetSession(NULL,1,INTERNET_OPEN_TYPE_PRECONFIG); 
pConnection=pSession-> GetHttpConnection(szServer,nPort,NULL,NULL); 
DWORD dwFlag=INTERNET_FLAG_EXISTING_CONNECT ¦INTERNET_FLAG_RELOAD ¦INTERNET_FLAG_DONT_CACHE; if(!bAutoRedirect) 

dwFlag=dwFlag ¦INTERNET_FLAG_NO_AUTO_REDIRECT; 

pHttpFile=pConnection-> OpenRequest(CHttpConnection::HTTP_VERB_POST,szObject,szServer,1,NULL,NULL,dwFlag); pHttpFile-> AddRequestHeaders("Content-Type: appliction/x-www-form-urlencoded",HTTP_ADDREQ_FLAG_ADD_IF_NEW,-1L); pHttpFile-> SendRequest(NULL,0,(LPVOID)szData,strlen(szData)); 
if(pHttpFile) 

if(pHttpFile-> QueryInfoStatusCode(dwHttpStatus)!=0) 

//dwHttpStatus=200; 
if(dwHttpStatus <400) 

int nRead=0; 
LPSTR pBuffer=new char[1024]; 
do{ 
nRead=pHttpFile-> Read(pBuffer,1023); 
if(nRead!=0) 

pBuffer[nRead]=0; 
csResponse+=pBuffer; 

}while(nRead!=0); 
if(pBuffer) 

delete pBuffer; 
pBuffer=NULL; 




}catch(CInternetException *e) 

e-> Delete(); 

catch(...) 
{ } 
if(pHttpFile!=NULL) 

pHttpFile-> Close(); 
delete pHttpFile; 

if(pConnection!=NULL) 

pConnection-> Close(); 
delete pConnection; 

if(pSession!=NULL) 

pSession-> Close(); 
delete pSession; 


  
我在下面的这个函数中响应按钮点击事件 
void CCMDlg::OnBnClickedOk() 

         
       CString csResponse;  
       CString sPhone; 
       GetDlgItemText(IDC_EDIT1,sPhone); 
INTERNET_PORT nPort=8080; 
DWORD dwRes=0; 
//AfxMessageBox(sPhone); 
CString strData="mobileform:"; 
strData+=sPhone; 
Post(csResponse,"www.ip138.com",nPort,"search.asp",strData,dwRes,TRUE); 
//AfxMessageBox(csResponse); 
SetDlgItemText(IDC_EDIT1,csResponse); 

 

解决方案 »

  1.   

    LPSTR pBuffer=new char[1024];  
    do{  
    nRead=pHttpFile->  Read(pBuffer,1023);  
    if(nRead!=0)  
    {  
    pBuffer[nRead]=0;  
    csResponse+=pBuffer;  
    }  
    }while(nRead!=0);  上面那个就是返回信息,每次读1023个字节,你根据nRead判断返回的多少个字节.  至于怎么读出来要看你对什么数据感兴趣了
      

  2.   

    1楼的你写的和我写的得到的结果是一样的,还是停留在http://www.ip138.com:8080/search.asp这个页面,没有得到返回信息的那个页面!不过还是感谢你的帮助!
      

  3.   

    这个。。
    俺只会用socket post数据,并且拿到结果。
      

  4.   

    另一贴已经回复:
    http://topic.csdn.net/u/20080306/15/a4d79896-3625-411f-bfdc-b3a0d6cf2649.html