错误C2664:' HttpOpenRequestW' :无法从< LPTSTR [2]'转换参数6到LPCWSTR

时间:2014-04-29 08:53:26

标签: c++ visual-c++

嗨,我的代码出现以下错误,这是针对json数据的发布请求

错误1错误C2664:' HttpOpenRequestW' :无法从< LPTSTR [2]'转换参数6到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 个答案:

答案 0 :(得分:3)

尝试使用:

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

而不是:

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