Task1 Description:
1.Install VC2005 and MSDN
2.Create a windows application to send out HTTP GET request
2.1 Functionalities
-2.1.1 User input url, app parse the url to get the host information
-2.1.2 Create a TCP connection with the host
-2.1.3 Send out TCP packet with HTTP GET command
(For example: the app will send out following content
according to the host information in step 2.1.1
GET / HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Host: WWW.YAHOO.COM.CN)
-2.1.4 Receive the response message from remote Host
(For example:
HTTP/1.1 301 Moved Permanently
Date: Sun, 06 Apr 2008 13:17:11 GMT
Location: http://cn.yahoo.com/
Cache-Control: private
Connection: close
Content-Type: text/html; charset=GBK)
-2.1.5 Close the TCP connection
请问大牛们,这个程序如何写? 希望能把关键代码写出来,谢谢
1.Install VC2005 and MSDN
2.Create a windows application to send out HTTP GET request
2.1 Functionalities
-2.1.1 User input url, app parse the url to get the host information
-2.1.2 Create a TCP connection with the host
-2.1.3 Send out TCP packet with HTTP GET command
(For example: the app will send out following content
according to the host information in step 2.1.1
GET / HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Host: WWW.YAHOO.COM.CN)
-2.1.4 Receive the response message from remote Host
(For example:
HTTP/1.1 301 Moved Permanently
Date: Sun, 06 Apr 2008 13:17:11 GMT
Location: http://cn.yahoo.com/
Cache-Control: private
Connection: close
Content-Type: text/html; charset=GBK)
-2.1.5 Close the TCP connection
请问大牛们,这个程序如何写? 希望能把关键代码写出来,谢谢
解决方案 »
- CListCtrl拖动水平滚动条,上面的子控件重复出现
- 为什么会报错呀?
- IDispatch接口中的GetTypeInfo函数如何被调用
- 用线程控制进度条 MFC
- 简单问题,但我不懂,求各位大哥,大姐帮忙啊
- 在非对话框类里如何动态创建对话框控件?40分加分
- 请问有没有这样的工具?我想把VCD中的*.DAT文件转成MP3的格式
- 如何在软件中实现插件?
- 小弟有一小段vc代码,请教哪位高手帮我翻译成vb代码,求救!!
- gloox怎么发送获取好友列表哦
- 毕业设计求救!!!Runtime Error! ... abnormal program termination
- 为何在没有安装vc2003的电脑中运行exe文件,出现应用程序错误
那么就发送"GET / HTTP/1.0\r\nHost:www.163.com\r\n\r\n"
其他的字段都可以省略
if( pHttpConn == NULL )
return IDS_ERR_CONNECT_FAILED;
CHttpFile* pHttpFile = pHttpConn->OpenRequest( NULL, sRequest );// Read the response string.
CString sResponse;
try
{
pHttpFile->SendRequest();
pHttpFile->ReadString( sResponse );
}
catch( CInternetException* e )
{}
pHttpFile->Close();
delete pHttpFile;
pHttpConn->Close();
delete pHttpConn;
CHttpConnection* pHttpConn = iNetSession.GetHttpConnection( sHostName, netPort, NULL, NULL );
if( pHttpConn == NULL )
return IDS_ERR_CONNECT_FAILED;
CHttpFile* pHttpFile = pHttpConn->OpenRequest( NULL, sRequest ); // Read the response string.
CString sResponse;
try
{
pHttpFile->SendRequest();
pHttpFile->ReadString( sResponse );
}
catch( CInternetException* e )
{ }
pHttpFile->Close();
delete pHttpFile;
pHttpConn->Close();
delete pHttpConn;
想问一下你的代码的是在什么开发环境的?我用的是VS2005里面的VC++,但是我找不到CHttpConnection、iNetSession和CHttpFile的类,请问该怎么办?
#include<windows.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")int main(int argc, char* argv[])
{
char server_name[256]= "www.163.com"; WSADATA wsaData;
struct sockaddr_in srv_addr; if (WSAStartup(0x202,&wsaData) == SOCKET_ERROR)
WSACleanup();
return -1; SOCKET cli_sock=socket(PF_INET,SOCK_STREAM,0);
if (cli_sock==INVALID_SOCKET)
return -1; // connect to server
unsigned short netshort;
if(WSAHtons(cli_sock, SOCK_STREAM, &netshort)) return -1; srv_addr.sin_family = AF_INET;
LPHOSTENT lphost = gethostbyname(server_name);
if (lphost != NULL)
srv_addr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
srv_addr.sin_port= netshort;
if (connect(cli_sock,(LPSOCKADDR)&srv_addr,sizeof(srv_addr))==SOCKET_ERROR){
return -1;
} char sz[]="GET / HTTP/1.1\r\nHost:www.163.com\r\n\r\n";
int retval = send(cli_sock,sz,sizeof(sz)-1,0);
if( SOCKET_ERROR == retval )
return 0;
char buffer[1024]={0};
while( SOCKET_ERROR != retval ){
retval = recv(cli_sock ,buffer,1000,0);
printf("%s\n",buffer);
}
closesocket(cli_sock);
return 0;
}
csReq = strVerb + m_csObject + " HTTP/1.1\r\n"; if ( !m_csUsername.IsEmpty () )
{
strAuth = _T("");
Base64Encode ( m_csUsername + ":" + m_csPassword, strAuth );
csReq += "Authorization: Basic " + strAuth + "\r\n";
}
CString csPort;
if ( m_nPort != DEFAULT_HTTP_PORT )
csPort.Format ( ":%d", m_nPort );
csReq += "Host: " + m_csServer + csPort + "\r\n";
csReq += "Accept: */*\r\n";
csReq += "Pragma: no-cache\r\n";
csReq += "Cache-Control: no-cache\r\n";
csReq += "User-Agent: "+m_csUserAgent+"\r\n";
if( m_csReferer.IsEmpty() )
{
m_csReferer = GetRefererFromURL ();
}
csReq += "Referer: "+m_csReferer+"\r\n";
csReq += "Connection: close\r\n";
if ( !m_csCookieFlag.IsEmpty() )
{
csReq += "Cookie: " + m_csCookieFlag + "\r\n";
}
// 指定要下载的文件范围
CString csEndPos;
int nWillDownloadStartPos = Get_WillDownloadStartPos (); // 开始位置
int nWillDownloadSize = Get_WillDownloadSize(); // 本次应该下载的字节数
int nDownloadedSize = Get_DownloadedSize (); // 已下载字节数
if ( nWillDownloadSize > 0 )
csEndPos.Format ( "%d", nWillDownloadStartPos+nWillDownloadSize-1 );
ASSERT ( nWillDownloadSize < 0 || nDownloadedSize < nWillDownloadSize );
strRange.Format ( _T("Range: bytes=%d-%s\r\n"), nWillDownloadStartPos+nDownloadedSize, csEndPos ); csReq += strRange;
csReq += "\r\n";