Solved

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

Posted on 2006-07-05
3
855 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

734 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