[英]Converting CURL request to HTTP Request Java
我有以下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
}
}
我不確定HttpURLConnection
是否是你最好的朋友。 我認為Apache HttpClient在這里是一個更好的選擇。
萬一你必須使用HttpURLConnection
,你可以試試這個鏈接:
您正在設置用戶名/密碼,HTTP-Header選項並忽略SSL證書驗證。
HTH
以下為我工作:
Authenticator.setDefault(new MyAuthenticator("user@account","password"));
-------
public MyAuthenticator(String user, String passwd){
username=user;
password=passwd;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.