资源描述:
《HTTP应用编程》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验九HTTP应用编程一、实验目的:1、了解HTTP报文格式及请求主要命令。2、熟悉HTTP工作原理。3、熟悉WinInet组成实现HTTP类及作用。4、掌握HTTP应用编程方法。二、主要仪器设备:l硬件:处理器:Pentium233MHz或更高处理器;内存:128MBRAM或更高;硬盘空间:至少130MB可用硬盘空间。l软件:WindowsXP、Professional等操作系统、VC++.NET2005。三、实验内容:设计一个HTTP客户端程序并编码实现:输入服务器地址、网页地址、网页名称后显示给网页的内容。四、实验步骤及结果:1、主要程序代码:#include"Std
2、Afx.h"#include"MyHTTPClient.h"constintBUF_SIZE=2048;CMyHTTPClient::CMyHTTPClient(void){//Setthetimeoutvalueto30secondsm_InternetSession.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,1000);m_InternetSession.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT,1000);m_InternetSession.SetOption(INTERNET_OPT
3、ION_SEND_TIMEOUT,1000);p_HttpConnection=NULL;m_hEventKill=CreateEvent(NULL,TRUE,FALSE,NULL);}CMyHTTPClient::~CMyHTTPClient(void){}boolCMyHTTPClient::Download(constCStringURLString,CString&strResponse,boolpbAbort){boolrr=false;//打开远程资源文件CStdioFile*pStdioFile=m_InternetSession.OpenURL(URLStr
4、ing);if(pStdioFile){TCHARbuffer[BUF_SIZE]={0};unsignedintnRead=BUF_SIZE;strResponse.Empty();//读取文件while((nRead==BUF_SIZE)&&(WaitForSingleObject(m_hEventKill,0)==WAIT_TIMEOUT)&&(!pbAbort)){//readremotedataintobuffernRead=pStdioFile->Read(buffer,BUF_SIZE);strResponse+=buffer;}//关闭远程资源文件pStdi
5、oFile->Close();deletepStdioFile;rr=true;}returnrr;}boolCMyHTTPClient::Download(constCStringsHost,constintnPort,constCStringstrObject,CString&strResponse,boolpbAbort){boolrr=false;try{//建立HTTP连接p_HttpConnection=m_InternetSession.GetHttpConnection(sHost,(INTERNET_PORT)nPort);//打开远程资源文件CHttpF
6、ile*pHttpFile=p_HttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET,strObject);if(pHttpFile){DWORDdwRet;pHttpFile->AddRequestHeaders("TestHTTP");pHttpFile->SendRequest();//发送请求pHttpFile->QueryInfoStatusCode(dwRet);//获取该请求的状态号以判断该请求成功与否if(dwRet==HTTP_STATUS_OK){TCHARbuffer[BUF_SIZE]=
7、{0};unsignedintnRead=BUF_SIZE;strResponse.Empty();//读取文件while((nRead==BUF_SIZE)&&(WaitForSingleObject(m_hEventKill,0)==WAIT_TIMEOUT)&&(!pbAbort)){nRead=pHttpFile->Read(buffer,BUF_SIZE);strResponse+=buffer;}}//关闭远程资源文件pHttpFile->Close();deletepHttpFile;rr=true;