1
votes

hi i got following error for my code which is for post request for json data

Error 1 error C2664: 'HttpOpenRequestW' : cannot convert parameter 6 from 'LPTSTR [2]' to 'LPCWSTR *' c:\users\gbsindia1\documents\visual studio 2010\projects\program\program\program1.cpp

#include "stdafx.h"
#include <windows.h>
#include <Wininet.h>
#include <stdio.h>
#include <tchar.h>


int main(int argc, char* argv[])




{
    HINTERNET hInternet = InternetOpen(_T("MyApp"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

    HINTERNET hConnect = InternetConnect(hInternet, _T("192.168.1.7"),
        INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);

    LPTSTR rgpszAcceptTypes[] = {_T("application/json"), NULL};
    HINTERNET hRequest = HttpOpenRequest(hConnect, _T("POST"), 
        _T(":8080/hellowword.jsp"), NULL, NULL, rgpszAcceptTypes, 0, 0); // here the error occure

    HttpAddRequestHeaders(hRequest, _T("Content-Type: application/json\r\n"), -1, 
        HTTP_ADDREQ_FLAG_ADD);

    char *JsonData = "..."; 
    HttpSendRequest(hRequest, NULL, 0, JsonData, strlen(JsonData));

    DWORD StatusCode = 0;
    DWORD StatusCodeLen = sizeof(StatusCode);
    HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &StatusCode, 
        &StatusCodeLen, NULL);

    if (StatusCode == 200)
    {
        // use InternetQueryDataAvailable() and InternetReadFile()
        // to read any response data as needed...
    }

    InternetCloseHandle(hRequest);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hInternet);
}
1
It says that you should compile project for UNICODE. - SChepurin

1 Answers

3
votes

try to use:

LPCWSTR rgpszAcceptTypes[] = {L"application/json", NULL};

instead of:

LPTSTR rgpszAcceptTypes[] = {_T("application/json"), NULL};