C ++无法连接到HTTP服务器 [英] C++ can't connect to HTTP server

查看:141
本文介绍了C ++无法连接到HTTP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取网页的HTML正文,但代码似乎无法通过HTTP工作。



www.google.com正常工作
http://www.Blogger.com不起作用

它既没有返回Blogger.com,也没有将页面移动到http的信息...





I'm trying to get a webpage's HTML body and the code just does not seem to work over HTTP.

www.google.com works
http://www.Blogger.com does not work
neither Blogger.com it returns but the info that the page has moved to http...


#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <string>
#include <locale>
#include "Web.h"

#pragma comment(lib,"ws2_32.lib")
using namespace std;


string Web::DownloadData(string url)
{
    //open website
    Connect(&url[0u]);

    //format website HTML
    for (size_t i=0; i<website_HTML.length(); ++i)
        website_HTML[i]= tolower(website_HTML[i],local);

    //display HTML
    cout <<website_HTML;

    cout<<"\n\n";

    return website_HTML;
}

void Web::Connect(char *url)
{
    WSADATA wsaData;
    SOCKET Socket;
    SOCKADDR_IN SockAddr;


    int lineCount=0;
    int rowCount=0;

    struct hostent *host;
    char *get_http= new char[256];

        memset(get_http,' ', sizeof(get_http) );
        strcpy(get_http,"GET / HTTP/1.1\r\nHost: ");
        strcat(get_http,url);
        strcat(get_http,"\r\nConnection: close\r\n\r\n");

        if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
        {
            cout << "WSAStartup failed.\n";
            system("pause");
            //return 1;
        }

        Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        host = gethostbyname(url);

        SockAddr.sin_port=htons(80);
        SockAddr.sin_family=AF_INET;
        SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);

        cout << "Connecting to "<< url<<" ...\n";

        if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0)
        {
            cout << "Could not connect";
            system("pause");
            //return 1;
        }

        cout << "Connected.\n";
        send(Socket,get_http, strlen(get_http),0 );

        char buffer[10000];

        int nDataLength;
            while ((nDataLength = recv(Socket,buffer,10000,0)) > 0)
            {
                int i = 0;

                while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r')
                {
                    website_HTML+=buffer[i];
                    i += 1;
                }
            }
        closesocket(Socket);
        WSACleanup();

        delete[] get_http;
}





我的尝试:



-------------------------------------------



What I have tried:

-------------------------------------------

推荐答案

所以代码工作,连接就在那里但是你得不到你想要的东西。



这是因为您正在原始HTTP级别与服务器通信。收到的数据将以您必须分析的HTTP响应开始。



如果您不想自己这样做,则必须使用执行此操作的库为您(网络客户端库)。



另请参阅 URL重定向 - 维基百科 [ ^ ]。它解释了发生了什么,并显示了使用HTPP标头和HTML内容进行此类重定向的示例。
So the code works and the connection is there but you just don't get what you want.

That is because you are communicating with the server on the raw HTTP level. The received data will begin with an HTTP response that you have to analyse.

If you don't want to do that yourself you have to use a library that does it for you (a web client library).

See also URL redirection - Wikipedia[^]. It explains what is happening and shows an example of such a redirection with the HTPP header and the HTML content.


这篇关于C ++无法连接到HTTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆