jak napisać rozkaz GET/POPST do coinmarketcap.com API w code blocks c++

0

Halo jak napisać rozkaz który pobierze dane z coinmarketcap.com API bez użycia jakiś dodatkowych bibliotek
Program jest napisany prawidłowo ale dostaję ten błąd
error:

<center><h1>400 Bad Request</h1></center>

Nie wiem co mam napisać w zmiennej, szukałem ale nigdzie nie znalazłem jak powinny wyglądać headery API:
string get_http =


dokumentacja:
//https://coinmarketcap.com/api/documentation/v1/#

//https://stackoverflow.com/questions/1011339/how-do-you-make-a-http-request-with-c
//http://www.cplusplus.com/forum/beginner/139313/
//https://aticleworld.com/http-get-and-post-methods-example-in-c/
//https://docs.microsoft.com/en-us/windows/desktop/api/wininet/nf-wininet-httpsendrequesta

//You can supply your API Key in REST API calls in one of two ways:

// Preferred method: Via a custom header named X-CMC_PRO_API_KEY
// Convenience method: Via a query string parameter named CMC_PRO_API_KEY

#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <vector>
#include <locale>
#include <sstream>

#include <string>
#include <stdio.h>
using namespace std;


#pragma comment(lib,"ws2_32.lib");
#pragma comment(lib,"libws2_32.a");
#pragma comment(lib,"lws2_32.a");


int main( void ){

WSADATA wsaData;
SOCKET Socket;
SOCKADDR_IN SockAddr;
int lineCount=0;
int rowCount=0;
struct hostent *host;
locale local;
char buffer[10000];
int i = 0 ;
int nDataLength;
string website_HTML;

// website url
//string url = "pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";
string key22 = "x";
string url5 = "pro-api.coinmarketcap.com";

//HTTP GET
string get_http = "GET /v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY="+key22+" HTTP/1.1\r\n'Host': pro-api.coinmarketcap.com\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(url5.c_str());
    cout<<"1"<<endl;
    //system("pause");

    SockAddr.sin_port=htons(80);
    cout<<"2"<<endl;
    //system("pause");
    SockAddr.sin_family=AF_INET;
    cout<<"22"<<endl;
    //system("pause");
    SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);

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

   cout<<"4"<<endl;
    //system("pause");
    // send GET / HTTP
    send(Socket,get_http.c_str(), strlen(get_http.c_str()),0 );
    //send(Socket,get_http2.c_str(), strlen(get_http2.c_str()),0 );
    //send(Socket,get_http.c_str(), strlen(get_http.c_str()),0 );

   cout<<"5"<<endl;
   // system("pause");
    // recieve html
    while ((nDataLength = recv(Socket,buffer,10000,0)) > 0){
        int i = 0;
        while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r'){

   //cout<<"7"<<endl;
    //system("pause");
            website_HTML+=buffer[i];
            i += 1;
        }
    }

   cout<<"8"<<endl;
   // system("pause");
    closesocket(Socket);
    WSACleanup();

   cout<<"9"<<endl;
    //system("pause");
    // Display HTML source
    cout<<website_HTML;

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


 return 0;
}

0

Dlaczego akurat wybrałeś C++ do tego?

0
pingwindyktator napisał(a):

Dlaczego akurat wybrałeś C++ do tego?

dlatego że ze studiów które przerwałem tylko podstawy c++ umiem a chcę wczytać ceny wszystkich krypto walut do pliku i potem dalej coś robić z tym.
edit: nie wiem wcale jak w c++ wygląda przesyłanie headerów do serwera.

0

Jeśli piszesz pod Windowsa, użyj funkcji z WinINet zamiast rzeźbić w surowych socketach. A jeśli Linux/Windows/whatever - libcurl.

0
0x666 napisał(a):

Jeśli piszesz pod Windowsa, użyj funkcji z WinINet zamiast rzeźbić w surowych socketach. A jeśli Linux/Windows/whatever - libcurl.

Już działa z WinINet


A stary program próbowalem na te 2 sposoby ale dostaję błąd
error:HTTP/1.1 301 Moved Permanently

string get_http = "GET /v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY=key&start=1&limit=5 HTTP/1.1\r\nHost: pro-api.coinmarketcap.com\r\nContent-Type: text/html\r\nAccept: */*\r\nConnection: close\r\n\r\n"; 

string get_http = "GET /v1/cryptocurrency/listings/latest?start=1&limit=5 HTTP/1.1\r\nHost: pro-api.coinmarketcap.com\r\nContent-Type: text/html\r\nX-CMC_PRO_API_KEY: key\r\nAccept: */*\r\nConnection: close\r\n\r\n"; 

ok prawdopodobnie łączem się poprzez http a ma być https


#include <iostream>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <vector>
#include <locale>
#include <sstream>

#include <string>
#include <stdio.h>

#include <tchar.h>
#include <wininet.h>

#pragma comment (lib, "wininet.lib")
#pragma comment (lib, "libwininet.a")

//https://stackoverflow.com/questions/10106816/how-to-contruct-httpsendrequest-method-of-wininet

using namespace std;

int main()
{
string website_HTML;

HINTERNET hIntSession =
  ::InternetOpen(_T("MyApp"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);

HINTERNET hHttpSession =
  InternetConnect(hIntSession, _T("pro-api.coinmarketcap.com"), 80, 0, 0, INTERNET_SERVICE_HTTP, 0, NULL);

HINTERNET hHttpRequest = HttpOpenRequest(
  hHttpSession,
  _T("GET"),
  _T("v1/cryptocurrency/listings/latest?start=1&limit=5"),
  0, 0, 0, INTERNET_FLAG_RELOAD, 0);

TCHAR* szHeaders = _T("Content-Type: text/html\r\nX-CMC_PRO_API_KEY: key");
CHAR szReq[1024] = "";
if( !HttpSendRequest(hHttpRequest, szHeaders, _tcslen(szHeaders), szReq, strlen(szReq))) {
  DWORD dwErr = GetLastError();
  /// handle error
}

CHAR szBuffer[1025];
DWORD dwRead=0;
while(::InternetReadFile(hHttpRequest, szBuffer, sizeof(szBuffer)-1, &dwRead) && dwRead) {
  szBuffer[dwRead] = 0;
  OutputDebugStringA(szBuffer);
  dwRead=0;
   int i = 0;
    while (szBuffer[i] >= 32 || szBuffer[i] == '\n' || szBuffer[i] == '\r'){
        website_HTML+=szBuffer[i];
        i += 1;
    }

}

::InternetCloseHandle(hHttpRequest);
::InternetCloseHandle(hHttpSession);
::InternetCloseHandle(hIntSession);

cout << website_HTML << endl;

return 0;

}

1 użytkowników online, w tym zalogowanych: 0, gości: 1