2016-10-07 8 views
1

Ich brauche Hilfe mit http bekommen. Ich benutze vsC++ 2015. Ich habe meinen eigenen Code, aber es scheint falsch zu sein, so bekomme ich Antwort vom Server in einem seltsamen Format. Beispiel: 034a0686bc29ca826dbb2f1ea4dd1879═Visual Studio 2015 C++ http Get & Post

so die Quelle ist:

#include "Connection.h" 

CConnector g_pConnector; 


CConnector::CConnector() { 

} 


CConnector::~CConnector() { 

} 

void CConnector::Connect(char* Url, char* Host) { m_Url = Url; m_Host = Host; } 

void CConnector::SendPOST(const char* data, std::string &sDestBuffer) {  try {  char httpUseragent[512];  DWORD szhttpUserAgent = sizeof(httpUseragent);  ObtainUserAgentString(0, httpUseragent, &szhttpUserAgent); 

     CString sMsg = "";  sMsg += /*\r\ncontent-type: application/x-www-form-urlencoded*/"\r\ncontent-type: application/x-www-form-urlencoded";  sMsg += /*\r\ncontent-lenght: 
*/"\r\ncontent-lenght: ";  sMsg += std::to_string(strlen(data)).c_str();  sMsg += /*\r\nuser-agent: secret_agent\r\n\r\n*/"\r\nuser-agent: secret_agent\r\n\r\n"; 

     HINTERNET internet = InternetOpenA(httpUseragent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);   if (internet != NULL)  { 

      HINTERNET connect = InternetConnectA(internet, m_Host, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);   if (connect != NULL)   { 

       HINTERNET request = HttpOpenRequestA(connect, /*POST*/"POST", m_Url, /*HTTP/1.1*/"HTTP/1.1", NULL, NULL, 
        INTERNET_FLAG_HYPERLINK | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | 
        INTERNET_FLAG_IGNORE_CERT_DATE_INVALID | 
        INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | 
        INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS | 
        INTERNET_FLAG_NO_AUTH | 
        INTERNET_FLAG_NO_CACHE_WRITE | 
        INTERNET_FLAG_NO_UI | 
        INTERNET_FLAG_PRAGMA_NOCACHE | 
        INTERNET_FLAG_RELOAD, NULL); 

       if (request != NULL) 
       { 
        int datalen = 0; 
        if (data != NULL) datalen = strlen(data); 
        int headerlen = 0; 
        if (!sMsg.IsEmpty()) headerlen = strlen(sMsg); 


        HttpSendRequestA(request, sMsg, headerlen, _strdup(data), datalen); 

        if (request) 
        { 
         DWORD dataSize = 0; 
         DWORD dwBytesRead = 0; 
         DWORD dwBytesWritten = 0; 
         char* data = NULL; 
         do { 

          char buffer[2000]; 
          InternetReadFile(request, (LPVOID)buffer, _countof(buffer), &dwBytesRead); 
          char *tempData = new char[dataSize + dwBytesRead]; 
          memcpy(tempData, data, dataSize); 
          memcpy(tempData + dataSize, buffer, dwBytesRead); 
          delete[] data; 
          data = tempData; 
          dataSize += dwBytesRead; 
         } while (dwBytesRead); 

         sDestBuffer = data; 

        } 

        InternetCloseHandle(request); 
       }   }   InternetCloseHandle(connect);  }  InternetCloseHandle(internet); } catch (...) {} } 

void CConnector::SendGET(const char* data, std::string &sDestBuffer) { try {  char httpUseragent[512];  DWORD szhttpUserAgent = sizeof(httpUseragent);  ObtainUserAgentString(0, httpUseragent, &szhttpUserAgent); 

     CString sMsg = "";  CString Url = m_Url;  Url += "?";   Url += data; 


     HINTERNET internet = InternetOpenA(httpUseragent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);   if (internet != NULL)  { 

      HINTERNET connect = InternetConnectA(internet, m_Host, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);   if (connect != NULL)   { 

       HINTERNET request = HttpOpenRequestA(connect, /*GET*/"GET", Url, /*HTTP/1.1*/"HTTP/1.1", NULL, NULL, 
        INTERNET_FLAG_HYPERLINK | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | 
        INTERNET_FLAG_IGNORE_CERT_DATE_INVALID | 
        INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | 
        INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS | 
        INTERNET_FLAG_NO_AUTH | 
        INTERNET_FLAG_NO_CACHE_WRITE | 
        INTERNET_FLAG_NO_UI | 
        INTERNET_FLAG_PRAGMA_NOCACHE | 
        INTERNET_FLAG_RELOAD, NULL); 

       if (request != NULL) 
       { 
        int datalen = 0; 
        if (data != NULL) datalen = strlen(data); 
        int headerlen = 0; 
        if (!sMsg.IsEmpty()) headerlen = strlen(sMsg); 


        HttpSendRequestA(request, sMsg, headerlen, _strdup(data), datalen); 

        if (request) 
        { 
         DWORD dataSize = 0; 
         DWORD dwBytesRead = 0; 
         DWORD dwBytesWritten = 0; 
         char* data = NULL; 
         do { 

          char buffer[2000]; 
          InternetReadFile(request, (LPVOID)buffer, _countof(buffer), &dwBytesRead); 
          char *tempData = new char[dataSize + dwBytesRead]; 
          memcpy(tempData, data, dataSize); 
          memcpy(tempData + dataSize, buffer, dwBytesRead); 
          delete[] data; 
          data = tempData; 
          dataSize += dwBytesRead; 
         } while (dwBytesRead); 

         sDestBuffer = data; 

        } 

        InternetCloseHandle(request); 
       }   }   InternetCloseHandle(connect);  } 

    InternetCloseHandle(internet); } catch (...) {} } 

und ich versuche immer die Antwort wie diese

std::string request; 
char key[128]; 
g_pConnector.Connect(script.php?something=139, "sample.com"); 
g_pConnector.SendGET(key, request); 
+0

Nur some funktioniert Ich bemerkte: Sie verwenden '_strdup', um eine nicht-konstante Kopie eines' const char * 'Puffers (' data' hier) zu erhalten, um an 'HttpOpenRequestA' zu übergeben. 'strdup' verwendet' malloc', um Speicher für den Puffer zu reservieren, den es zurückgibt, also sollten Sie es nach dem Gebrauch "frei" machen - aber dieser Code verliert nur den Zeiger und niemals 'free'. Das ist aber nicht das Problem, viel Glück. –

Antwort

0

Sie Sc0rn psilent Methode verwenden müssen, um es

+0

Was? Ich verstehe dich nicht ... – namespaceT