I have the following code in Java, which I want to convert in Delphi. To get a response in JSON below java code is used to get token from the server. The code in eclipse has the desired result, but when I try to convert the code in Delphi, I'm getting a response from the server of an "illegal user".
I believe the mistake is in sending the request. Is the below code doing the right request in Delphi?
Java code:
headerMap.put("Content-Type", "application/x-www-form-urlencoded");
private static void sendPost(Map<String, String> headerMap, Map<String, String> paramMap) {
try {
HttpPost post = new HttpPost(openapi_url);
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (String key : paramMap.keySet()) {
list.add(new BasicNameValuePair(key, paramMap.get(key)));
}
post.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
if (null != headerMap) {
post.setHeaders(assemblyHeader(headerMap));
}
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity, "utf-8"));
} catch (IOException e) {
System.err.println(e);
}
}
/**
*
* @param headers
* @return
*/
private static Header[] assemblyHeader(Map<String, String> headers) {
Header[] allHeader = new BasicHeader[headers.size()];
int i = 0;
for (String str : headers.keySet()) {
allHeader[i] = new BasicHeader(str, headers.get(str));
i++;
}
return allHeader;
}
Delphi Code:
ParamList := TStringList.Create();
for item in Dictionary do
begin
ParamList.Add(item.Key + '=' + item.Value);
end;
for I := 0 to Dictionary.Count-1 do begin
Memo1.Lines.Add(ParamList.Strings[I]);
end;
try
lHTTP := TIdHTTP.Create;
lHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
lHTTP.Request.CharSet := 'utf-8';
try
memo2.Lines.Text := lHTTP.Post(openapi_url, ParamList);
except
on E: Exception do
ShowMessage('Error on request: '#13#10 + e.Message);
end
finally
lHTTP.Free;
ParamList.Free;
end;