Solved

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

Posted on 2006-07-05
3
845 Views
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);
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
Comment
Question by:ablazso
3 Comments
 
LVL 86

Accepted Solution

by:
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;
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 17048622
alternatively, check out sprintf() :

http://www.cplusplus.com/ref/cstdio/sprintf.html
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
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
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now