Solved

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

Posted on 2006-07-05
3
856 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
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.
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.

688 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