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.   

    教你个偷懒的方法,装一个wget,运行时只需要使用wget -d的方式把调试信息输出出来
      

  2.   

    假如你请求的是www.163.com
    那么就发送"GET / HTTP/1.0\r\nHost:www.163.com\r\n\r\n"
    其他的字段都可以省略
      

  3.   

    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;
      

  4.   

    楼上的高手:
    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的类,请问该怎么办?
      

  5.   


    #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;
    }
      

  6.   

    CString csReq, strAuth, strRange;
    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";
      

  7.   

    参考Visual C++网络高级编程
      

  8.   

    http://topic.csdn.net/u/20080116/09/d1d4b6bc-0f55-4da8-bb67-6781350993f0.html