且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 CURL 请求转换为 HTTP 请求 Java

更新时间:2022-06-13 22:35:16

有很多方法可以实现这一点.我认为下面是最简单的,同意它不是很灵活但有效.

There are numerous ways to achieve this. Below one is simplest in my opinion, Agree it isn't very flexible but works.

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

    }
}