Is there a better way to create a long URL parameter in C++?

Posted on 2006-07-05
Last Modified: 2010-05-18
Are there better ways than multiple strcpy-s and strconcat-s to assemble a long parameter? This long parameter is needed by the web service (it is a strange WSDL less service) I need to use.

Here is an excerpt from my program:

using namespace MSXML2;     // For Msxml3.dll.
using namespace std;
HRESULT hr = CoInitialize(NULL);
_bstr_t test = "user=Fah8tdCFWZrQ&password=tv3WKK5f4vnfWZPp&version=3.1&test_request=True&delim=True&delim_char=|&relay=False&first_name=Alex&last_name=Gincola&address=101 Main&city=Phonenix&state=AZ&zip=60525&amount=100&currency=USD&method=CHECK&type=AUTH&aba=122000661&bank_acct_num=0624100154&bank_acct_type=CHECKING&bank_name=Bank Of America&acct_name=Alex Blazso&trx_type=WEB";
try {
     IServerXMLHTTPRequestPtr pXMLHTTPReq = NULL;
     hr = pXMLHTTPReq.CreateInstance(__uuidof(ServerXMLHTTP30));      
     hr = pXMLHTTPReq->open("POST", "https://secure.aaaa/bbbb/cccc.dll", VARIANT_TRUE);                  
         hr = pXMLHTTPReq->setRequestHeader("Content-Type", "applications/x-www-form-urlencoded");
         hr = pXMLHTTPReq->send(test);
         long readyState = READYSTATE_UNINITIALIZED;            
         MSG msg;            
         while (readyState != READYSTATE_COMPLETE) {
            if (PeekMessage(&msg, 0, 0 ,0, PM_REMOVE)) {
                 TranslateMessage (&msg);
                 DispatchMessage (&msg);
            readyState = pXMLHTTPReq->GetreadyState();      
         MessageBox(NULL, "readyState is COMPLETE ", "readystate", MB_OK);
       _bstr_t tempStr = pXMLHTTPReq->responseText;
       const char* lpstr = (LPCSTR)tempStr;
         MessageBox(NULL, lpstr, "readystate", MB_OK);
         int inLen = strlen(lpstr);
         printf ("\nrespData=[%s] len=[%d]", lpstr, inLen);
         long status= pXMLHTTPReq->Getstatus();
Question by:ablazso
LVL 86

Accepted Solution

jkr earned 250 total points
ID: 17047596
Sure, use a std::string so you can take advantage of  the operators 'operator+()' and/or 'operator+=()', e.g.

#include <string>
using namespace std;

string strUrl ="user=Fah8tdCFWZrQ";
strUrl += "&password=tv3WKK5f4vnfWZPp";
strUrl += "&version=3.1&";
//... etc.

_bstr_t test = strUrl.c_str();


BTW, the same should work with a CString, e.g

CString strUrl ="user=Fah8tdCFWZrQ";
strUrl += "&password=tv3WKK5f4vnfWZPp";
strUrl += "&version=3.1&";
//... etc.

_bstr_t test = (LPCTSTR) strUrl;
LVL 53

Expert Comment

ID: 17048622
alternatively, check out sprintf() :
LVL 39

Expert Comment

ID: 17048712
alternatively, use stringstream. That allows to concatenate different types as in sprintf but is safe. You can concatenate any type that has a overload of of operator<<.

#include <sstream>
#include <string>
using namespace std;

    ostringstream oss;
    oss << "That is a long string that contains the number " << 12345 << " and text.":
    // you can get the concatenated string by

    const char* psz = oss.str().c_str();

Regards, Alex

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question