【问题标题】:HTTP POST by converting Java to Delphi using Indy通过使用 Indy 将 Java 转换为 Delphi 的 HTTP POST
【发布时间】:2018-12-07 16:18:57
【问题描述】:

我有以下 Java 代码,我想在 Delphi 中进行转换。要在 JSON 中获得响应,下面的 java 代码用于从服务器获取令牌。 eclipse 中的代码有想要的结果,但是当我尝试在 Delphi 中转换代码时,我得到了来自“非法用户”的服务器的响应。

我认为错误在于发送请求。下面的代码在 Delphi 中做正确的请求吗?

Java 代码:

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;
}

德尔福代码:

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;

【问题讨论】:

  • 首先检查您发送到服务器的字符串。 IE。 headers,我认为 header 格式应该是name:value\r\nW3C
  • 您显示的 Delphi 代码很好,就准备 POST 参数列表而言。但是您的 Java 代码还可以选择设置 HTTP 标头,而您的 Delphi 代码没有这样做。您可以使用TIdHTTP.Request.CustomHeaders 属性添加TIdHTTP.Request 子属性尚未涵盖的任何自定义标头。例如,身份验证凭据/令牌。此外,还有一点需要记住 - 一些服务器对 User-Agent 请求标头很敏感,因此您可能需要设置 TIdHTTP.Request.UserAgent 以模仿 HttpPost 或真正的 Web 浏览器会发送的内容。

标签: java json api delphi-10.2-tokyo


【解决方案1】:

@Remy Lebeau 建议是解决问题的关键,基于此链接 [https://stackoverflow.....are-query-string-keys-case-sensitive][1] 我的请求返回数据(即:LowerCase(md5hash('password')))可能对某人有所帮助。谢谢,你们是救世主。

 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多