用CSocket怎么连接到www.sina.com.cn?

解决方案 »

  1.   

    为什么要连接到sina?!
    问怎么连接到www.csdn.net不是很好吗。
      

  2.   

    CSocket *pSocket;
    char    Request[100];
    char    Response[2000]; pSocket = new CSocket();
    pSocket->Create();
    pSocket->Bind(INADDR_ANY,sizeof(SOCKADDR));
    pSocket->Connect("www.sina.com.cn",80);
    if (pSocket->GetLastError() == 0)
    {
                       MessageBox("成功连接到 www.sina.com.cn");
    strcpy(Request,"GET http://www.sina.com.cn/index.html HTTP/1.1\nAccept:text/text");
    pSocket->Send(Request,lstrlen(Request));
    memset(Response,0,500);
    pSocket->Receive(Response,1999);
    if (pSocket->GetLastError() == 0)
    MessageBox(Response,"服务器响应");
    } pSocket->Close();
    delete pSocket; 向服务器发出的GET请求格式是错误的,所以服务器不会响应,但是确实是连接到服务器了。
      

  3.   

    iicup(双杯献酒) :
    为什么要连接到sina?!
    问怎么连接到www.csdn.net不是很好吗。
    愤怒的青年,我欣赏
      

  4.   

    看了下书,正确地向Web服务器发送请求的代码应该是:if (pSocket->GetLastError() == 0)
    {
    MessageBox("成功连接到 www.sina.com.cn");
    strcpy(Request,"GET /index.html HTTP/1.0\r\n\r\n");
    pSocket->Send(Request,lstrlen(Request));
    memset(Response,0,2000);
    pSocket->Receive(Response,1999);
    if (pSocket->GetLastError() == 0)
    MessageBox(Response,"服务器响应");
    }