Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <windows.h>
- #include <winhttp.h>
- #include "cJSON.h"
- const wchar_t *conttype[] = { L"application/json", NULL };
- int main(int argc, char **argv)
- {
- HINTERNET hsession = NULL;
- wchar_t ipaddr[0x40];
- unsigned portno;
- wchar_t target[0x40];
- mbstowcs(ipaddr, argv[1], sizeof(ipaddr));
- portno = atoi(argv[2]);
- mbstowcs(target, argv[3], sizeof(target));
- hsession = WinHttpOpen(L"test",
- WINHTTP_ACCESS_TYPE_NO_PROXY,
- WINHTTP_NO_PROXY_NAME,
- WINHTTP_NO_PROXY_BYPASS, NULL);
- if (!hsession) return 1;
- while (1)
- {
- HINTERNET hconnect = NULL,
- hrequest = NULL;
- char *message;
- size_t meslen;
- wchar_t respbuf[0x1000];
- size_t respsz = sizeof(respbuf);
- cJSON *json;
- char operation[0x40];
- char secret[0x40];
- printf("operation (insert/delete/quit): ");
- scanf("%s", operation);
- if (0 == strcmp(operation, "quit")) break;
- printf("secret: ");
- scanf("%s", secret);
- json = cJSON_CreateObject();
- cJSON_AddStringToObject(json, "secret", secret);
- cJSON_AddStringToObject(json, "operation", operation);
- if (!strcmp(operation, "insert"))
- {
- char plate[0x40],
- owner[0x40],
- period[0x40];
- printf("plate: ");
- scanf("%s", plate);
- printf("owner: ");
- scanf("%s", owner);
- printf("period: ");
- scanf("%s", period);
- cJSON_AddStringToObject(json, "plate", plate);
- cJSON_AddStringToObject(json, "owner", owner);
- cJSON_AddNumberToObject(json, "period", atof(period));
- }
- else if (!strcmp(operation, "delete"))
- {
- char plate[0x40];
- printf("plate: ");
- scanf("%s", plate);
- cJSON_AddStringToObject(json, "plate", plate);
- }
- else goto skip;
- message = cJSON_Print(json);
- meslen = strlen(message);
- hconnect = WinHttpConnect(hsession, ipaddr, portno, 0);
- if (!hconnect) return 2;
- hrequest = WinHttpOpenRequest(hconnect, L"POST", target, NULL,
- WINHTTP_NO_REFERER, conttype, 0);
- if (!hrequest) return 3;
- if (!WinHttpSendRequest(hrequest,
- L"Content-Type: application/json\n", -1L,
- message, meslen, meslen, NULL)) return 4;
- puts("response:");
- WinHttpReceiveResponse(hrequest, NULL);
- WinHttpQueryHeaders(hrequest, WINHTTP_QUERY_RAW_HEADERS_CRLF,
- WINHTTP_HEADER_NAME_BY_INDEX,
- respbuf, &respsz,
- WINHTTP_NO_HEADER_INDEX);
- wprintf(L"%ls", respbuf);
- WinHttpQueryDataAvailable(hrequest, &respsz);
- WinHttpReadData(hrequest, respbuf, respsz, &respsz);
- printf("%ls\n", respbuf);
- WinHttpCloseHandle(hrequest);
- WinHttpCloseHandle(hconnect);
- free(message);
- skip:
- cJSON_Delete(json);
- }
- WinHttpCloseHandle(hsession);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement