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;
@Remy Lebeau recommendation is the key to solve the issue and based on this link [ https://stackoverflow.....are-query-string-keys-case-sensitive][1] my request return the data (ie: LowerCase(md5hash('password'))) may be help someone. Thanks, guys you are the savior.
lHTTP := TIdHTTP.Create;
try
lHTTP.Request.UserAgent := 'Apache-HttpClient/4.5.3 (Java/1.8.0_60)'; //this save me
lHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
lHTTP.Request.Connection:= 'Keep-Alive';
lHTTP.HTTPOptions:= [hoKeepOrigProtocol];
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.