문제

I have an app made in Delpi XE2 that acts as a web server, that I need to pass POST/GET variables for it to process em.

To pass the parameter I use this code(php):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html charset=utf-8" />
</head>
<?php
$url = "http://85.59.95.216:8080?data='тест'";
$timeout = 10;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_close($ch);
?>

and then in delphi:

procedure TMain.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
    Memo1.Lines.Add('Accesing: '+ARequestInfo.Document);
    Memo1.Lines.Add('Method: '+ARequestInfo.Command);
    Memo1.Lines.Add('Params: '+ARequestInfo.UnparsedParams);
end;

and the response I get is:

Accesing: /
Method: GET
Params: data='ýýýýýýýý'

How can I convert it to something readable?

Sorry if this is an easy question, first time doing a web server in delphi.

도움이 되었습니까?

해결책

Non-ASCII characters are not allowed in URLs. Your PHP code is telling Curl to send a UTF-8 encoded URL without encoding the non-ASCII octets in the required %hh notation. Indy's default charset is ASCII (by design) so it will lose non-ASCII octets it receives.

On the PHP side, you have to use curl_escape() or urlencode() to encode reserved/forbidden characters in a URL, CURLOPT_URL will not do that encoding for you.

Then on the Indy side, you can use the ARequestInfo.Params property to accessed the parsed and decoded data if the TIdHTTPServer.ParseParams property is True. The ARequestInfo.UnparsedParams and ARequestInfo.QueryParams properties contain raw unparsed data that you would have to parse and decode manually.

Update A better option is to use a POST request to send your data using the application/x-www-form-urlencoded content type. That allows a charset to be specified for the encoded data. TIdHTTPServer will then decode the data into the TIdHTTPRequestInfo.FormParams property. Look at curl's CURLOPT_POST and CURLOPT_POSTFIELDS options to send an application/x-www-form-urlencoded formatted request.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top