Advertisement
Guest User

Untitled

a guest
Mar 5th, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <windows.h>
  5. #include <winhttp.h>
  6. #include "cJSON.h"
  7.  
  8. const wchar_t *conttype[] = { L"application/json", NULL };
  9.  
  10. int main(int argc, char **argv)
  11. {
  12.     HINTERNET hsession = NULL;
  13.  
  14.     wchar_t ipaddr[0x40];
  15.     unsigned portno;
  16.     wchar_t target[0x40];
  17.  
  18.     mbstowcs(ipaddr, argv[1], sizeof(ipaddr));
  19.     portno = atoi(argv[2]);
  20.     mbstowcs(target, argv[3], sizeof(target));
  21.      
  22.     hsession = WinHttpOpen(L"test",
  23.                            WINHTTP_ACCESS_TYPE_NO_PROXY,
  24.                            WINHTTP_NO_PROXY_NAME,
  25.                            WINHTTP_NO_PROXY_BYPASS, NULL);
  26.  
  27.     if (!hsession) return 1;
  28.  
  29.     while (1)
  30.     {
  31.         HINTERNET hconnect = NULL,
  32.                   hrequest = NULL;
  33.        
  34.         char *message;
  35.         size_t meslen;
  36.  
  37.         wchar_t respbuf[0x1000];
  38.         size_t respsz = sizeof(respbuf);
  39.  
  40.         cJSON *json;
  41.         char operation[0x40];
  42.         char secret[0x40];
  43.  
  44.         printf("operation (insert/delete/quit): ");
  45.         scanf("%s", operation);
  46.  
  47.         if (0 == strcmp(operation, "quit")) break;
  48.  
  49.         printf("secret: ");
  50.         scanf("%s", secret);
  51.  
  52.         json = cJSON_CreateObject();
  53.         cJSON_AddStringToObject(json, "secret", secret);
  54.         cJSON_AddStringToObject(json, "operation", operation);
  55.  
  56.         if (!strcmp(operation, "insert"))
  57.         {
  58.             char plate[0x40],
  59.                  owner[0x40],
  60.                  period[0x40];
  61.        
  62.             printf("plate: ");
  63.             scanf("%s", plate);
  64.  
  65.             printf("owner: ");
  66.             scanf("%s", owner);
  67.  
  68.             printf("period: ");
  69.             scanf("%s", period);
  70.  
  71.             cJSON_AddStringToObject(json, "plate", plate);
  72.             cJSON_AddStringToObject(json, "owner", owner);
  73.             cJSON_AddNumberToObject(json, "period", atof(period));
  74.         }
  75.         else if (!strcmp(operation, "delete"))
  76.         {
  77.             char plate[0x40];
  78.  
  79.             printf("plate: ");
  80.             scanf("%s", plate);
  81.  
  82.             cJSON_AddStringToObject(json, "plate", plate);
  83.         }
  84.         else goto skip;
  85.  
  86.         message = cJSON_Print(json);
  87.         meslen = strlen(message);
  88.  
  89.         hconnect = WinHttpConnect(hsession, ipaddr, portno, 0);
  90.  
  91.         if (!hconnect) return 2;
  92.  
  93.         hrequest = WinHttpOpenRequest(hconnect, L"POST", target, NULL,
  94.                                       WINHTTP_NO_REFERER, conttype, 0);
  95.  
  96.         if (!hrequest) return 3;
  97.  
  98.         if (!WinHttpSendRequest(hrequest,
  99.                                 L"Content-Type: application/json\n", -1L,
  100.                                 message, meslen, meslen, NULL)) return 4;
  101.  
  102.         puts("response:");
  103.  
  104.         WinHttpReceiveResponse(hrequest, NULL);
  105.         WinHttpQueryHeaders(hrequest, WINHTTP_QUERY_RAW_HEADERS_CRLF,
  106.                                       WINHTTP_HEADER_NAME_BY_INDEX,
  107.                                       respbuf, &respsz,
  108.                                       WINHTTP_NO_HEADER_INDEX);
  109.  
  110.         wprintf(L"%ls", respbuf);
  111.  
  112.         WinHttpQueryDataAvailable(hrequest, &respsz);
  113.         WinHttpReadData(hrequest, respbuf, respsz, &respsz);
  114.  
  115.         printf("%ls\n", respbuf);
  116.  
  117.         WinHttpCloseHandle(hrequest);
  118.         WinHttpCloseHandle(hconnect);
  119.  
  120.         free(message);
  121.  
  122.         skip:
  123.         cJSON_Delete(json);
  124.     }
  125.  
  126.     WinHttpCloseHandle(hsession);
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement