SteveFarndon2000
asked on
Using POST with Indy 10 TIdHTTP
Hello Experts,
This will be simple for the one who's done this before. It's driving me nuts trying to spot the right method/property to make this work. The Indy 10 documentation is ...er...weak on examples.
All I want to do is to have a TIdHTTP object (Indy 10 that came with Delphi 2006) do a POST request. Please see below the code I have tried so far. I just get "HTTP /1.1 500 Internal server error" when it tries to execute h.post.
I'm losing too much time hence the high points tariff.
Please help!
This will be simple for the one who's done this before. It's driving me nuts trying to spot the right method/property to make this work. The Indy 10 documentation is ...er...weak on examples.
All I want to do is to have a TIdHTTP object (Indy 10 that came with Delphi 2006) do a POST request. Please see below the code I have tried so far. I just get "HTTP /1.1 500 Internal server error" when it tries to execute h.post.
I'm losing too much time hence the high points tariff.
Please help!
function TTAService.SendSMS(const Destination: WideString; const Body: WideString): Integer;
var
h : TIdHTTP;
URL: string;
Params: TStringStream;
res: TStringStream;
begin
try
h := TIdHTTP.create()
Params := TStringStream.Create('');
Params.WriteString(HTTPEncode('Billing_Ref=KUDEX'+'&'));
Params.WriteString(HTTPEncode('DestinationEx='+Destination+'&'));
Params.WriteString(HTTPEncode('Body='+Body+'));
URL := 'http://ws.textanywhere.net/HTTPRX/SendSMSEx.aspx';
h.Request.Method := 'POST';
h.Request.Source := Params;
h.Request.ContentType := 'application/x-www-form-urlencoded';
res := TStringStream.create('');
h.ConnectTimeout := 20000;
h.post(URL,params,res);
except
on E: Exception do begin
ShowMessage('Error: ' + E.Message);
end;
end;
Params.Free;
h.free()
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the solution. You get the points because you implicitly identified that the 'HTTPEncode' function was the problem although it would have been more convincing if you had said this out loud. Some more explanation about how the TIdHTTP object works in relation to posting would have been even better (to show understanding).
Thanks again for your help.
Thanks again for your help.
@SteveFarndon2000
My method failed?,
if case let me know error plz
My method failed?,
if case let me know error plz
ASKER
Mahdi78,
Both solutions worked but neither one of you explained what I was doing wrong or why your solutions would fix the problem. I had to differentiate between the pair of you and Eddie Shipman did (implicitly) highlight the URL encoding issue. I think his solution shows a bit more understanding, and remember that your solution is dependent on the .AddFormField method being bug-free (have you fully tested it?) which is something that Indy components are not reknown for.
Both solutions worked but neither one of you explained what I was doing wrong or why your solutions would fix the problem. I had to differentiate between the pair of you and Eddie Shipman did (implicitly) highlight the URL encoding issue. I think his solution shows a bit more understanding, and remember that your solution is dependent on the .AddFormField method being bug-free (have you fully tested it?) which is something that Indy components are not reknown for.
Thank you for you reply
To understand my method you will create post.php and put in localhost root, so your post URL will be "http://localhost/post.php"
copy the following script in post.php file
<?
echo '<b>Your username:</b>'.$_POST['use r_name']
.' <br><b>Your password:</b> '. $_POST['password'];
?>
and use Delphi code
uses IdMultipartFormData;
procedure TForm1.Button1Click(Sender : TObject);
var data: TIdMultiPartFormDataStream ;
begin
data := TIdMultiPartFormDataStream .Create;
try
data.AddFormField('user_na me', 'value1');
data.AddFormField('passwor d', 'value2');
Memo1.Text :=IdHTTP1.Post('http://localhost/post.php', data);
finally
data.Free;
end;
end;
To understand my method you will create post.php and put in localhost root, so your post URL will be "http://localhost/post.php"
copy the following script in post.php file
<?
echo '<b>Your username:</b>'.$_POST['use
.' <br><b>Your password:</b> '. $_POST['password'];
?>
and use Delphi code
uses IdMultipartFormData;
procedure TForm1.Button1Click(Sender
var data: TIdMultiPartFormDataStream
begin
data := TIdMultiPartFormDataStream
try
data.AddFormField('user_na
data.AddFormField('passwor
Memo1.Text :=IdHTTP1.Post('http://localhost/post.php', data);
finally
data.Free;
end;
end;
post method will reply with the following html code
<b>Your username:</b>value1 <br><b>Your password:</b> value2
<b>Your username:</b>value1 <br><b>Your password:</b> value2
My children were requiring Uia 1025 form last year and were informed about a business that hosts 6,000,000 forms . If people have been needing Uia 1025 form too , here's <code>http://goo.gl/mj9f3C</code>
you will use memo to receive response of your request
Open in new window