Thread: HTTP GET problems

  1. #1
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26

    HTTP GET problems

    I'm trying to send GET request to generate receiving address (Blockchain api, https://blockchain.info/ru/api/api_receive) On the page i've generated request - "
    Code:
    /api/receive?method=create&cors=true&format=plain&address=1A8JiWcwvpY7tAopUkSnGuEYHmzGYfZPiq&shared=false&callback=http%3A%2F%2Fgoogle.com
    and need to receive an answer -
    Code:
    {"callback_url":"Google","input_address":"16ofW1jBBLbVnDQWJsyL6NKq7hKms9tDLP","destination":"1A8JiWcwvpY7tAopUkSnGuEYHmzGYfZPiq","fee_percent":0}"
    I found sample code and trying to send request
    Code:
    SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        struct hostent *host;
        host = gethostbyname("www.google.com");
        SOCKADDR_IN SockAddr;
        SockAddr.sin_port = htons(80);
        SockAddr.sin_family = AF_INET;
        SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
        std::cout << "Connecting...\n";
        if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
            std::cout << "Could not connect";
            return -1;
        }
        std::cout << "Connected.\n";
        send(Socket, "GET /api/receive?method=create&cors=true&format=plain&address=1A8JiWcwvpY7tAopUkSnGuEYHmzGYfZPiq&shared=false&callback=http://microsoft.com/ HTTP/1.1\r\nHost: blockchain.info\r\nConnection: close\r\n\r\n", strlen("GET /api/receive?method=create&cors=true&format=plain&address=1A8JiWcwvpY7tAopUkSnGuEYHmzGYfZPiq&shared=false&callback=http://microsoft.com/ HTTP/1.1\r\nHost: blockchain.info\r\nConnection: close\r\n\r\n"), 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') {
                std::cout << buffer[i];
                i += 1;
            }
        }
        closesocket(Socket);
        return 1;
    }
    but receive 404 error. What is the problem and what is the best way to parse an answer (I need to get "input_address")? Thanks for help!

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    ^^, I didn't look at your sample code.

    I bet trying to generate this network request using `curl` is a walk in the park, much easier than trying to do it with someone else's code.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    as a side note, Sockets are not the best way to make HTTP connection.
    Sockets are for raw two-way bytes streaming, they are a bit low level for HTTP connections.
    and if you do plan on using sockets - at least put some headers in your request. many servers simply ignore your request if the request won't include basic headers like encoding, length, etc.
    if you're on windows, use WinHTTP API (I don't know the equivilant API on linux or Mac unfortunatly).

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Try connecting to blockchain.info instead of google.com, lol

    Also, don't use WinHTTP.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HTTP POST Request in C++ - header problems
    By phummon in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2010, 09:28 AM
  2. Help! Http
    By ytaipsw in forum Networking/Device Communication
    Replies: 5
    Last Post: 03-11-2009, 06:59 PM
  3. Some help with http (?)
    By tezcatlipooca in forum Networking/Device Communication
    Replies: 3
    Last Post: 05-12-2007, 06:37 AM
  4. Http
    By Scarvenger in forum Networking/Device Communication
    Replies: 6
    Last Post: 08-16-2006, 12:39 PM
  5. Http
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 06-22-2002, 04:05 AM