0

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 - "

/api/receive?method=create&cors=true&format=plain&address=1A8JiWcwvpY7tAopUkSnGuEYHmzGYfZPiq&shared=false&callback=http%3A%2F%2Fgoogle.com

and need to receive an answer -

{"callback_url":"http://google.com","input_address":"16ofW1jBBLbVnDQWJsyL6NKq7hKms9tDLP","destination":"1A8JiWcwvpY7tAopUkSnGuEYHmzGYfZPiq","fee_percent":0}"

I found sample code and trying to send request

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!

Lannser
  • 19
  • 3
  • problem was in "gethostbyname("www.google.com")" string. – Lannser Sep 05 '15 at 16:02
  • I changed it to blockchain.info and it solves a problem. But i still cant parse server answer. help plz – Lannser Sep 05 '15 at 16:03
  • hey, i'm close to solution! i found an answer here (http://stackoverflow.com/questions/4551898/separating-http-response-body-from-header-in-c) and change code to this: int i = 0; while (buffer[i] != '\r' || buffer[i + 1] != '\n' || buffer[i + 2] != '\r' || buffer[i + 3] != '\n') i += 1; while (i – Lannser Sep 05 '15 at 16:12
  • You are ignoring `nDataLength`, which tells you how many bytes are actually in the `buffer`. And you are ignoring the streaming nature of TCP, there is no guarantee that you will have the complete HTTP response (or even complete CRLF pairs) in the `buffer`. What you have shown is not a valid HTTP implementation at all. There are numerous examples of how to implement HTTP (I have posted several myself), or else use an HTTP library, such as libcurl. – Remy Lebeau Sep 05 '15 at 16:56
  • when i print all answer, i found httponly error. i have to receive cookie to bypass it?! – Lannser Sep 05 '15 at 17:06

0 Answers0