0

I need to put cs value inside the bracket for the object client. I have already handle the cs variable like this:

CString cs (bar_->GetHostName());

But still fail to put the value inside this below code:

WinHttpClient client(/* dont know what value to put here */);   
client.SendHttpRequest();

Error message:

Error   1   error C2664: 'WinHttpClient::WinHttpClient(const std::wstring &)' : cannot convert parameter 1 from 'const char [15]' to 'const std::wstring &' c:\test.cpp

edit :

   CString cs (bar_->GetHostName());
   WinHttpClient client(/*??*/);
   client.SendHttpRequest();
   wstring httpResponseHeader = client.GetHttpResponseHeader();
   wstring httpResponse = client.GetHttpResponse();
   writeToStorage(httpResponse.c_str());

My function writeToStorage suppose to write something to a textfile. It seems, overwriting the file with no strings in it.. I dont know why :(

karikari
  • 6,627
  • 16
  • 64
  • 79

4 Answers4

1

You need to provide a std::wstring.

This question explains the conversions: How to convert CString and ::std::string ::std::wstring to each other?

Community
  • 1
  • 1
Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
1

Looks like you are using MFC's CString and you want to pass cs to the WinHttpClient.

Here is how you do it:

CString cs (bar_->GetHostName());
std::string s((LPCTSTR)cs);
WinHttpClient client(s);   
client.SendHttpRequest();

If WinHttpClient has an overloaded constructor that accepts wchar*, then you can do it like this:

CString cs (bar_->GetHostName());
WinHttpClient client(cs);   
client.SendHttpRequest();

However, if you want to pass plain text as mentioned in some other answers, then do this:

WinHttpClient client(_T("Text"));   
client.SendHttpRequest();

In Visual C++, using _T is a better option than using L

Aamir
  • 14,882
  • 6
  • 45
  • 69
0

Write WinHttpClient client(L"text"); instead of WinHttpClient client("text");

Abyx
  • 12,345
  • 5
  • 44
  • 76
-1

Abyx is right. You should to write L before the string. But it works only with constant strings. L is a macros that converts char[] to wchar_t[] - Unicode char array. For dynamic converting you have to use MultiByteToWideChar (...) function.