RESTful API身份验证 – 来自JavaFX桌面客户端应用程序

huangapple go评论107阅读模式
英文:

RESTful API Authentication from JavaFX Desktop Client Application

问题

我目前正在开发一个使用JavaFX + FXML编写的桌面应用程序,我需要连接到一个我用NodeJS编写的远程RESTful API。我一直在尝试寻找一个更新的参考资料,适用于JDK 11及以上版本,但看起来大多数我找到的好的参考资料要么是关于Spring的,要么是至少3年前编写的。

有人能指点我正确的方向吗?谢谢。

英文:

I'm currently developing a desktop application written in JavaFX + FXML and I need to connect to a remote RESTful API I wrote in NodeJS. I've been trying to find an updated reference catering to JDK 11 and up but it looks like most good references I find are either in Spring or were written at least 3 years ago.

Can anybody point me in the right direction? Thanks.

答案1

得分: 1

这是我到目前为止想出的。与我习惯使用的AJAX和Axios相比,它感觉原始而啰嗦,但仍然能够工作:

  1. private void btnLoginClick() throws IOException {
  2. CloseableHttpClient httpclient = HttpClients.createDefault();
  3. HttpPost httpPost = new HttpPost("http://localhost:8000/api/auth");
  4. httpPost.addHeader("accept", "application/json");
  5. List<NameValuePair> params = new ArrayList<NameValuePair>();
  6. params.add(new BasicNameValuePair("email", username.getText()));
  7. params.add(new BasicNameValuePair("password", password.getText()));
  8. httpPost.setEntity(new UrlEncodedFormEntity(params));
  9. try {
  10. CloseableHttpResponse response = httpclient.execute(httpPost);
  11. HttpEntity entity = response.getEntity();
  12. String data = EntityUtils.toString(entity);
  13. JSONObject json = new JSONObject(data);
  14. EntityUtils.consume(entity);
  15. response.close();
  16. // 在这里进行更多处理...
  17. } catch (IOException e) {
  18. Alert a = new Alert(AlertType.ERROR);
  19. a.setContentText("登录:无法连接到服务器。检查您的连接,或稍后再试。要报告此错误,请联系 m@jhourlad.com。");
  20. a.show();
  21. } finally {
  22. httpclient.close();
  23. }
  24. }
英文:

This is what I have come up so far. It feels primitive and chatty compared to I am used to using AJAX and Axios but works nevertheless:

  1. private void btnLoginClick() throws IOException {
  2. CloseableHttpClient httpclient = HttpClients.createDefault();
  3. HttpPost httpPost = new HttpPost(&quot;http://localhost:8000/api/auth&quot;);
  4. httpPost.addHeader(&quot;accept&quot;, &quot;application/json&quot;);
  5. List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
  6. params.add(new BasicNameValuePair(&quot;email&quot;, username.getText()));
  7. params.add(new BasicNameValuePair(&quot;password&quot;, password.getText()));
  8. httpPost.setEntity(new UrlEncodedFormEntity(params));
  9. try {
  10. CloseableHttpResponse response = httpclient.execute(httpPost);
  11. HttpEntity entity = response.getEntity();
  12. String data = EntityUtils.toString(entity);
  13. JSONObject json = new JSONObject(data);
  14. EntityUtils.consume(entity);
  15. response.close();
  16. // Do more processing here...
  17. } catch (IOException e) {
  18. Alert a = new Alert(AlertType.ERROR);
  19. a.setContentText(&quot;Login: Unable to connect to server. Check your connection or try at a later time. To report this error please contact m@jhourlad.com.&quot;);
  20. a.show();
  21. } finally {
  22. httpclient.close();
  23. }
  24. }

huangapple
  • 本文由 发表于 2020年8月29日 12:20:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63643389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定