将CURL请求转换为HTTP请求Java

我有以下CURL请求,谁能请我确认subesquest HTTP请求" title="HTTP请求">HTTP请求是什么?

      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。

提前致谢。

苏维

回答:

有很多方法可以实现这一目标。在我看来,以下一项是最简单的,同意它不是很灵活,但是可以工作。

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

}

}

以上是 将CURL请求转换为HTTP请求Java 的全部内容, 来源链接: utcz.com/qa/434806.html

回到顶部