将CURL请求转换为HTTP请求Java

use*_*807 13 java curl

我有以下CURL请求任何人都可以请确认我的subesquest HTTP请求

      curl -u "Login-dummy:password-dummy" -H "X-Requested-With: Curl" "https://qualysapi.qualys.eu/api/2.0/fo/report/?action=list" -k
Run Code Online (Sandbox Code Playgroud)

它会是什么样的?

    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
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我完全将上述curl请求转换为httpreq.

提前致谢.

苏维

Ash*_*rat 17

有很多方法可以实现这一目标.在我看来,一个是最简单的,同意它不是很灵活但是有效.

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

    }
}
Run Code Online (Sandbox Code Playgroud)