我需要将java代码翻译成delphi [英] I need to translate java code to delphi

查看:99
本文介绍了我需要将java代码翻译成delphi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拥有以下java代码:



Have the following java code:

// Set your API key: remember to change this to your live API key in production
    String apiKeyUserName = "your api username";
    String apiKeyPassword = "your api password";

    // Set the query params
    DefaultHttpClient httpclient = new DefaultHttpClient();

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("fromPostcode", "2000"));
    params.add(new BasicNameValuePair("toPostcode", "3000"));
    params.add(new BasicNameValuePair("networkId", "01"));
    params.add(new BasicNameValuePair("lodgementDate", "2013-08-01"));
    String query = URLEncodedUtils.format(params, "UTF-8");

    String urlPrefix = "api.auspost.com.au";
    String validateURL = "https://" + urlPrefix + "/DeliveryDates.xml?";

    HttpGet httpGet = new HttpGet(validateURL + query);
    httpGet.addHeader("Cookie", "OBBasicAuth=fromDialog");
    httpGet.addHeader(BasicScheme.authenticate(
        new UsernamePasswordCredentials(apiKeyUserName, apiKeyPassword),
        "US-ASCII",false));

HttpResponse response = httpclient.execute(httpGet);





我尝试过:



想把它翻译成Delphi。对于我使用TIdHttp的请求如下:





What I have tried:

Want to translate this into an Delphi. For the request I used TIdHttp as below:

procedure RequestDeliveryDate;
var
  IdHTTP: TIdHTTP;
  LHandler: TIdSSLIOHandlerSocketOpenSSL;
  lResponse: String;
  requestURL: String;
begin
  IdHTTP := TIdHTTP.Create();
  IdHTTP.Request.BasicAuthentication := True;
  IdHTTP.Request.Username := 'your api username';
  IdHTTP.Request.Password := 'your api password';
  IdHTTP.Request.CharSet := 'utf-8';

  LHandler := TIdSSLIOHandlerSocketOpenSSL.Create();
  IdHTTP.IOHandler := LHandler;

  requestURL := 'https://api.auspost.com.au/DeliveryDates.xml?fromPostcode=2000' +
                                                            '&toPostcode=3000' +
                                                            '&networkId=01' +
                                                            '&lodgementDate=2018-02-23' +
                                                            '&numberOfDates=01';
  screen.Cursor := crHourGlass;
  try
    lResponse := IdHTTP.Get(requestURL);
    screen.Cursor := crDefault;
  except
    on E: Exception do
    begin
      screen.Cursor := crDefault;
      ShowMessage(E.Message);
    end;
  end;
  IdHTTP.Free;
end;





假设我提供了正确的api用户名和密码。当我调用上面的代码时,我收到以下错误:此服务器无法验证您是否有权访问所请求的文档。您提供了错误的凭据(例如,密码错误),或者您的浏览器不明白如何提供所需的凭证。



我做错了什么?有什么建议吗?



Let's presume that I do supply the right api user name and password. When I call the above code I get the following error: "This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required."

What I'm doing wrong? Any suggestion?

推荐答案

检查一下:

如何在Delphi中使用http身份验证打开URL? - 堆栈溢出 [ ^ ]

http - 如何在Delphi中添加Indy的Authorization = Bearer标题? - 堆栈溢出 [ ^ ]
Check this:
How open a url with http authentication in Delphi? - Stack Overflow[^]
http - How to add a "Authorization=Bearer" header with Indy in Delphi? - Stack Overflow[^]


似乎正在使用CookieManager,还必须在提供的地方添加@ auspost.com.au API密钥......
It appears that is working with the CookieManager and also had to add '@auspost.com.au' to the provided API Key ...


这篇关于我需要将java代码翻译成delphi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆