0

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;
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Fiaz
  • 30
  • 2
  • 9
  • Check the string you are sending into server first. I.e. headers, I think header format should be `name:value\r\n`. [W3C](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) – Victor Gubin Dec 07 '18 at 16:28
  • 1
    The Delphi code you have shown is fine, as far as preparing the POST params list. But your Java code is also optionally setting HTTP headers, which your Delphi code is not doing. You can use the `TIdHTTP.Request.CustomHeaders` property to add any custom headers that are not already covered by `TIdHTTP.Request` sub-properties. For instance, authentication credentials/tokens. Also, something else to keep in mind - some servers are sensitive to the `User-Agent` request header, so you might need to set the `TIdHTTP.Request.UserAgent` to mimic what `HttpPost` or a real web browser would send. – Remy Lebeau Dec 07 '18 at 18:40

1 Answers1

0

@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 (i.e: 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;
Fiaz
  • 30
  • 2
  • 9