C++ skicka variabel php (wininet)

Trädvy Permalänk
Medlem
Registrerad
Maj 2012

C++ skicka variabel php (wininet)

Hej, vet inte riktigt hur jag ska lösa problemet som jag kan fuska in 2 string variabler (username och secretKey) som skickar värdet till samma .php fil som används vid uppladdningen.

Har försökt kollat på andra exempel och implementera en fungerade lösning, men inte riktigt lyckats.

Sista delen av koden som tar hand om uppladdningen av (bilden) ser ut följande:

// PNG, upload BOOL uploadFile(HWND hwnd, LPCTSTR fileName) { const TCHAR* UPLOAD_SERVER = _T("192.168.0.101"); const TCHAR* UPLOAD_PATH = _T("/imago.php"); const char* sBoundary = "----BOUNDARYBOUNDARY----"; // boundary const char sCrLf[] = { 0xd, 0xa, 0x0 }; // (CR+LF) const TCHAR* szHeader = _T("Content-type: multipart/form-data; boundary=----BOUNDARYBOUNDARY----"); std::ostringstream buf; std::string idStr; // ID // ID idStr = getId(); // -- "id" part buf << "--"; buf << sBoundary; buf << sCrLf; buf << "content-disposition: form-data; name=\"id\""; buf << sCrLf; buf << sCrLf; buf << idStr; buf << sCrLf; // -- "imagedata" part buf << "--"; buf << sBoundary; buf << sCrLf; buf << "content-disposition: form-data; name=\"imagedata\"; filename=\"thefilepark.com\""; buf << sCrLf; //buf << "Content-type: image/png"; //buf << sCrLf; buf << sCrLf; // PNG std::ifstream png; png.open(fileName, std::ios::binary); if (png.fail()) { MessageBox(hwnd, _T("PNG open failed"), szTitle, MB_ICONERROR | MB_OK); png.close(); return FALSE; } buf << png.rdbuf(); // read all & append to buffer png.close(); buf << sCrLf; buf << "--"; buf << sBoundary; buf << "--"; buf << sCrLf; std::string oMsg(buf.str()); // WinInet (proxy) HINTERNET hSession = InternetOpen(szTitle, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if(NULL == hSession) { MessageBox(hwnd, _T("Cannot configure wininet"), szTitle, MB_ICONERROR | MB_OK); return FALSE; } HINTERNET hConnection = InternetConnect(hSession, UPLOAD_SERVER, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL); if(NULL == hSession) { MessageBox(hwnd, _T("Cannot initiate connection"), szTitle, MB_ICONERROR | MB_OK); return FALSE; } HINTERNET hRequest = HttpOpenRequest(hConnection, _T("POST"), UPLOAD_PATH, NULL, NULL, NULL, INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD, NULL); if(NULL == hSession) { MessageBox(hwnd, _T("Cannot compose post request"), szTitle, MB_ICONERROR | MB_OK); return FALSE; } // User-Agent const TCHAR* ua = _T("User-Agent: Imagowin/1.0\r\n"); BOOL bResult = HttpAddRequestHeaders( hRequest, ua, _tcslen(ua), HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE); if (FALSE == bResult) { MessageBox(hwnd, _T("Cannot set user agent"), szTitle, MB_ICONERROR | MB_OK); return FALSE; } if (HttpSendRequest(hRequest, szHeader, lstrlen(szHeader), (LPVOID)oMsg.c_str(), (DWORD) oMsg.length())) { DWORD resLen = 8; TCHAR resCode[8]; // status code HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE, resCode, &resLen, 0); if( _ttoi(resCode) != 200 ) { // upload, (status error) MessageBox(hwnd, _T("Failed to upload (unexpected result code, under maintainance?)"), szTitle, MB_ICONERROR | MB_OK); } else { // upload succeeded // get new id DWORD idLen = 100; TCHAR newid[100]; memset(newid, 0, idLen*sizeof(TCHAR)); _tcscpy_s(newid, _T("X-Imago-Id")); HttpQueryInfo(hRequest, HTTP_QUERY_CUSTOM, newid, &idLen, 0); if (GetLastError() != ERROR_HTTP_HEADER_NOT_FOUND && idLen != 0) { //save new id saveId(newid); } // (URL) DWORD len; char resbuf[1024]; std::string result; while(InternetReadFile(hRequest, (LPVOID) resbuf, 1024, &len) && len != 0) { result.append(resbuf, len); } // NULL terminate result += '\0'; setClipBoardText(result.c_str()); // URL execUrl(result.c_str()); return TRUE; } } else { MessageBox(hwnd, _T("Failed to upload"), szTitle, MB_ICONERROR | MB_OK); } return FALSE; }

Har inte hög kunskap kring varken c++ programmering eller wininet/php, ett exempel skulle vara "greatly appreciated".

Applikationens source code är följande: https://github.com/gyazo/Gyazowin
Kan även ladda upp min egen source code, om efterfråga finns. (VS Community 2015)

Speldator: [Intel Core i7 4790K] [ASUS Matrix GTX 780Ti x2] [ASUS MAXIMUS VII HERO] [Corsair Dominator Platinum 32GB] [Windows 7] [Logitech G400S] [Microsoft Ergonomic 4000] [BenQ XR3501 35" (HDMI)] [BenQ G900W 19" (VGA)]

Serverdator: [Intel Core i7 6850K] [ASUS X99-E WS] [Corsair Vengeance LPX 64GB] [2x Crucial MX300 1TB] [Cooler Master V1200] [CentOS 7]