Discussion:
IdHTTP Get with XML
(too old to reply)
Stephen
2007-07-30 15:19:35 UTC
Permalink
I'm attempting to validate addresses and zipcode through the post office's
server.

When I cut and copy the complete url to my web browser, I get a sucessful
verification, but when I attempt to programatically get the data, I fail.
Any pointers and example code would be greatly appreciated it, I'm really
new to this.

The result of my test is "400 Bad request".

My URL is (user ID changed):
http://testing.shippingapis.com/ShippingAPITest.dll?API=Verify&XML=<?xml
version='1.0'?><AddressValidateRequest USERID='999xxxxx9999'><Address
ID='0'><Address1></Address1><Address2>6406 Ivy
Lane</Address2><City>Greenbelt</City><State>md</State><Zip5>74429</Zip5><Zip4></Zip4></Address></AddressValidateRequest>

My code is:
AnsiString sString = "?API=Verify&XML=<?xml
version='1.0'?><AddressValidateRequest USERID='999xxxxx9999'><Address
ID='0'><Address1></Address1><Address2>6406 Ivy
Lane</Address2><City>Greenbelt</City><State>md</State><Zip5>74429</Zip5><Zip4></Zip4></Address></AddressValidateRequest>";

TIdHTTP* http = new TIdHTTP(Application);
http->Request->ContentType = "text/xml";

TStringStream* outstr = new TStringStream("");

try {
http->Get(sString, outstr);
ShowMessage(outstr->DataString);

}

__finally {
delete outstr;

}


Thank you for any pointers / help
Remy Lebeau (TeamB)
2007-07-30 17:10:52 UTC
Permalink
Post by Stephen
when I attempt to programatically get the data, I fail.
That is because you are not sending the request correctly, as the server is
telling you.
<snip>

You should be encoding that URL before passing it to Get().
Post by Stephen
AnsiString sString = "?API=Verify&XML=<?xml
<snip>

You are not including the full URL address in that string, only the query
portion. You must provide the full URL.
Post by Stephen
http->Request->ContentType = "text/xml";
You are sending the XML through the URL itself, not through a post stream,
so the ContentType is meaningless in this case.


Try this instead:

AnsiString sString =
"http://testing.shippingapis.com/ShippingAPITest.dll?API=Verify&XML=<?xml
version=\"1.0\"?><AddressValidateRequest USERID=\"999xxxxx9999\"><Address
ID=\"0\"><Address1></Address1><Address2>6406 Ivy
Lane</Address2><City>Greenbelt</City><State>md</State><Zip5>74429</Zip5><Zip
4></Zip4></Address></AddressValidateRequest>";

sString = TIdURI::URLEncode(__classid(TIdURI), sString);

TIdHTTP* http = new TIdHTTP(NULL);
try
{
ShowMessage(http->Get(sString));
}
__finally {
delete http;
}


Gambit
Stephen
2007-07-30 19:21:18 UTC
Permalink
Thanks for replying Remy, and I apologize for showing my ignorance. I know
next to nothing about making internet requests.

I've changed the code as you indicated and now get "Connection closed
gracefully" but never get a string (XML) value back. Any idea why? I've
passed a TStringStream as a second parameter. the working portion of my
code now looks like...

TStringStream *out = new TStringStream("");
http->Get(sString, out);
ShowMessage(out->DataString);

but it's crashing right after the get request.

Thanks again Remy.

Stephen
Post by Remy Lebeau (TeamB)
Post by Stephen
when I attempt to programatically get the data, I fail.
That is because you are not sending the request correctly, as the server is
telling you.
<snip>
You should be encoding that URL before passing it to Get().
Post by Stephen
AnsiString sString = "?API=Verify&XML=<?xml
<snip>
You are not including the full URL address in that string, only the query
portion. You must provide the full URL.
Post by Stephen
http->Request->ContentType = "text/xml";
You are sending the XML through the URL itself, not through a post stream,
so the ContentType is meaningless in this case.
AnsiString sString =
"http://testing.shippingapis.com/ShippingAPITest.dll?API=Verify&XML=<?xml
version=\"1.0\"?><AddressValidateRequest USERID=\"999xxxxx9999\"><Address
ID=\"0\"><Address1></Address1><Address2>6406 Ivy
Lane</Address2><City>Greenbelt</City><State>md</State><Zip5>74429</Zip5><Zip
4></Zip4></Address></AddressValidateRequest>";
sString = TIdURI::URLEncode(__classid(TIdURI), sString);
TIdHTTP* http = new TIdHTTP(NULL);
try
{
ShowMessage(http->Get(sString));
}
__finally {
delete http;
}
Gambit
Stephen
2007-08-01 21:54:49 UTC
Permalink
Any more ideas?

Thanks,
Stephen
Post by Stephen
Thanks for replying Remy, and I apologize for showing my ignorance. I
know next to nothing about making internet requests.
I've changed the code as you indicated and now get "Connection closed
gracefully" but never get a string (XML) value back. Any idea why? I've
passed a TStringStream as a second parameter. the working portion of my
code now looks like...
TStringStream *out = new TStringStream("");
http->Get(sString, out);
ShowMessage(out->DataString);
but it's crashing right after the get request.
Thanks again Remy.
Stephen
Post by Remy Lebeau (TeamB)
Post by Stephen
when I attempt to programatically get the data, I fail.
That is because you are not sending the request correctly, as the server is
telling you.
<snip>
You should be encoding that URL before passing it to Get().
Post by Stephen
AnsiString sString = "?API=Verify&XML=<?xml
<snip>
You are not including the full URL address in that string, only the query
portion. You must provide the full URL.
Post by Stephen
http->Request->ContentType = "text/xml";
You are sending the XML through the URL itself, not through a post stream,
so the ContentType is meaningless in this case.
AnsiString sString =
"http://testing.shippingapis.com/ShippingAPITest.dll?API=Verify&XML=<?xml
version=\"1.0\"?><AddressValidateRequest USERID=\"999xxxxx9999\"><Address
ID=\"0\"><Address1></Address1><Address2>6406 Ivy
Lane</Address2><City>Greenbelt</City><State>md</State><Zip5>74429</Zip5><Zip
4></Zip4></Address></AddressValidateRequest>";
sString = TIdURI::URLEncode(__classid(TIdURI), sString);
TIdHTTP* http = new TIdHTTP(NULL);
try
{
ShowMessage(http->Get(sString));
}
__finally {
delete http;
}
Gambit
Loading...