Simon Coenen
Simon Coenen

Reputation: 71

Simple HTTP Get with Winsock

Some time ago, I made a very simple method that could make an http request and return a response. This worked like I intended but now I want to use it again but I've suddenly ran into an issue I can't seem to figure out. Every time I make a request, it returns error 503: Service Unavailable.

Sometimes it works, sometimes it doesn't.

I noticed if I put a breakpoint after the shutdown of the connection and wait just a little and continue, there is no problem and everything works. So my guess is that this has something to do with timing/delay of the server.

Does anyone have experience with this?

Thanks in advance!

string HttpHelper::HttpGet(string host, string path)
{
    WSADATA wsaData;
    int result;

    // Initialize Winsock
    result = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (result != 0)
        throw SocketError(L"WSAStartUp", result);

    // Resolve the server address and port
    addrinfo * pAddrInfo;
    result = getaddrinfo(host.c_str(), "80", 0, &pAddrInfo);
    if (result != 0)
        throw SocketError(L"addrinfo", result);

    //Create the socket
    SOCKET sock = socket(pAddrInfo->ai_family, pAddrInfo->ai_socktype, pAddrInfo->ai_protocol);

    if (sock == INVALID_SOCKET)
        throw SocketError(L"Socket", WSAGetLastError());

    // Connect to server.
    result = connect(sock, pAddrInfo->ai_addr, pAddrInfo->ai_addrlen);
    if (result != 0)
        throw SocketError(L"Connect", WSAGetLastError());

    const string request = "GET " + path + " HTTP/1.1\nHost: " + host + "\n\n";

    // Send an initial buffer
    result = send(sock, request.c_str(), request.size(), 0);
    if (result == SOCKET_ERROR)
        throw SocketError(L"Send", WSAGetLastError());

    // shutdown the connection since no more data will be sent
    result = shutdown(sock, SD_SEND);
    if (result == SOCKET_ERROR)
        throw SocketError(L"Close send connection", WSAGetLastError());

    // Receive until the peer closes the connection
    string response;

    char buffer[50];
    int bytesRecv = 0;

    for (;;)
    {
        result = recv(sock, buffer, sizeof(buffer), 0);
        if (result == SOCKET_ERROR)
            throw SocketError(L"Recv", WSAGetLastError());
        if (result == 0)
            break;
        response += string(buffer, result);
        wstringstream stream;
        stream << L"HttpGet() > Bytes received: " << bytesRecv;
        DebugLog::Log(stream.str(), LogType::INFO);
        bytesRecv += result;
    }
    wstringstream stream;
    stream << L"HttpGet() > Bytes received: " << bytesRecv;
    DebugLog::Log(stream.str(), LogType::INFO);

    // cleanup
    result = closesocket(sock);
    if (result == SOCKET_ERROR)
        throw SocketError(L"Closesocket", WSAGetLastError());

    result = WSACleanup();
    if (result == SOCKET_ERROR)
        throw SocketError(L"WSACleanup", WSAGetLastError());

    freeaddrinfo(pAddrInfo);

    DebugLog::Log(L"HttpGet() > Cleanup Successful ", LogType::INFO);

    return response;
}

wstring SocketError::ErrorMessage(const wstring & context, const int errorCode) const
{
    wchar_t buf[1024];
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, errorCode, 0, buf, sizeof(buf), 0);
    wchar_t * newLine = wcsrchr(buf, '\r');
    if (newLine) *newLine = '\0';
    wstringstream stream;
    stream << L"Socket error in " << context << L" (" << errorCode << L"): " << buf;
    return stream.str();
}

Upvotes: 2

Views: 19791

Answers (3)

user9503966
user9503966

Reputation:

You must send it in this format for windows tcp sockets:

GET /romeo.txt HTTP/1.1\r\nHost: data.pr4e.org\r\n\r\n

where /romeo.txt is the path from host url and data.pr4e.org is host

Credits to someone :)

Upvotes: 3

selbie
selbie

Reputation: 104514

I'm going to assume that if you put that same host name into a web browser, you get back valid content and not a 503.

Three random guesses:

1) You need a User-Agent header. Build your request string as:

 const string request = "GET " + path + " HTTP/1.1\n"
 request += "Host: " + host + "\n";
 request += "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36\n";
 request += "\n";

2) And as I mentioned in my comments, make sure send is sending all the data:

size_t sent = 0;
size_t remaining = request.size();
while (remaining > 0)
{
    const char* data = request.c_str() + sent;
    result = send(sock, data, remaining, 0);
    if (result == SOCKET_ERROR)
            throw SocketError(L"Send", WSAGetLastError());
    sent += result;
    remaining -= result;
}

3)The only other thing might be the the shutdown call.

Just remove the following line. In theory, it shouldn't make a difference. But it's worth a try.

result = shutdown(sock, SD_SEND);

Upvotes: 1

Software_Designer
Software_Designer

Reputation: 8587

Hard to say...

Here is a simple and generic piece of winsock code you can try, just to see if the problem also exists when using this code.

#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <vector>
#include <locale>
#include <sstream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")


string website_HTML;
locale local;
void get_Website(string url );
char buffer[10000];
int i = 0 ;


 //****************************************************

int main( void ){

    get_Website("www.stackoverflow.com" );

    cout<<website_HTML;

    cout<<"\n\nPress ANY key to close.\n\n";
    cin.ignore(); cin.get(); 


 return 0;
}

 //****************************************************

void get_Website(string url ){
    WSADATA wsaData;
    SOCKET Socket;
    SOCKADDR_IN SockAddr;
    int lineCount=0;
    int rowCount=0;
    struct hostent *host;
    string get_http;


    get_http = "GET / HTTP/1.1\r\nHost: " + url + "\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.c_str());

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

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

    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();

}

Upvotes: 5

Related Questions