我有以下 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
会不会是这样的?
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
}
}
关于java - 将 CURL 请求转换为 HTTP 请求 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18636567/