将CURL请求转换为HTTP请求Java
我有下面的CURL请求,请任何人确认一下subesquesthttp请求是什么将CURL请求转换为HTTP请求Java,java,curl,Java,Curl,我有下面的CURL请求,请任何人确认一下subesquesthttp请求是什么 curl -u "Login-dummy:password-dummy" -H "X-Requested-With: Curl" "https://qualysapi.qualys.eu/api/2.0/fo/report/?action=list" -k 会是这样吗 String url = "https://qualysapi.qualys.eu/api/2.0/fo/report/";
curl -u "Login-dummy:password-dummy" -H "X-Requested-With: Curl" "https://qualysapi.qualys.eu/api/2.0/fo/report/?action=list" -k
会是这样吗
String url = "https://qualysapi.qualys.eu/api/2.0/fo/report/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET"); ..... //incomplete
谁能帮我把上面的curl请求完全转换成httpreq呢
提前谢谢
Suvi我不确定
HttpURLConnection
是否是您最好的朋友。我认为这是一个更好的选择
为了防止您必须使用HttpURLConnection
,您可以尝试以下链接:
您正在设置用户名/密码、HTTP头选项和忽略SSL证书验证
- 如何设置用户名/密码检查此线程:
- 如何设置eader选项查看以下线程:
- 这里介绍了如何忽略SSL证书验证
有很多方法可以实现这一点。在我看来,下面的一个是最简单的,我同意它不是很灵活,但很有效
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.apache.commons.codec.binary.Base64;
public class HttpClient {
public static void main(String args[]) throws IOException {
String stringUrl = "https://qualysapi.qualys.eu/api/2.0/fo/report/?action=list";
URL url = new URL(stringUrl);
URLConnection uc = url.openConnection();
uc.setRequestProperty("X-Requested-With", "Curl");
String userpass = "username" + ":" + "password";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);
InputStreamReader inputStreamReader = new InputStreamReader(uc.getInputStream());
// read this input
}
}
下面是我的工作:
Authenticator.setDefault(new MyAuthenticator("user@account","password"));
-------
public MyAuthenticator(String user, String passwd){
username=user;
password=passwd;
}
您可以从以下位置为org.apache.commons.codec.binary.Base64添加maven依赖项:commons codec commons codec 1.9