本文介绍了JAVA 中的 cURL 等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是为开源 JAVA 应用程序编写身份验证组件.我们有一个使用 https 的内部身份验证小部件.我有一些示例 php 代码,它访问使用 cURL 处理传输的 widget.

I am tasked with writing an authentication component for an open source JAVA app. We have an in-house authentication widget that uses https. I have some example php code that accesses the widget which uses cURL to handle the transfer.

我的问题是是否有 cURLJAVA 的端口,或者更好的是,什么基础包能让我足够接近来处理任务?

My question is whether or not there is a port of cURL to JAVA, or better yet, what base package will get me close enough to handle the task?

更新:

简而言之,我想在JAVA中复制的代码:

This is in a nutshell, the code I would like to replicate in JAVA:

$cp = curl_init();
$my_url = "https://" . AUTH_SERVER . "/auth/authenticate.asp?pt1=$uname&pt2=$pass&pt4=full";
curl_setopt($cp, CURLOPT_URL, $my_url);
curl_setopt($cp, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($cp);
curl_close($cp);

Heath,我认为你在正确的轨道上,我认为我我将最终使用 HttpsURLConnection,然后从响应中挑选出我需要的内容.

Heath, I think you're on the right track, I think I'm going to end up using HttpsURLConnection and then picking out what I need from the response.

推荐答案

省略了异常处理:

HttpURLConnection con = (HttpURLConnection) new URL("https://www.example.com").openConnection();
con.setRequestMethod("POST");
con.getOutputStream().write("LOGIN".getBytes("UTF-8"));
con.getInputStream();

这篇关于JAVA 中的 cURL 等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:44