• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 863
  • Last Modified:

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

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);
IXMLHTTPRequestPtr pXMLHTTPReq = 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();
.
.
.
0
ablazso
Asked:
ablazso
1 Solution
 
jkrCommented:
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;
0
 
Infinity08Commented:
alternatively, check out sprintf() :

http://www.cplusplus.com/ref/cstdio/sprintf.html
0
 
itsmeandnobodyelseCommented:
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now