Convert CURL Request to Java HTTP Request

I have the following CURL request. Can someone please acknowledge what will be the subesquest HTTP request.

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

Will it be something like?

  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 

Can anyone please be kind enough to help me completely convert the curl request above to httpreq.

Thanks in advance.

Suvi

+7
java curl
source share
3 answers

There are many ways to achieve this. In my opinion, the lowest is the easiest, you must admit that it is not very flexible, but it 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 } } 
+12
source

I'm not sure your best friend is here HttpURLConnection . I think Apache HttpClient is the best option.

Just in case, you should use HttpURLConnection , you can try the following links:

You set the username / password, the HTTP header parameter, and ignore the confirmation of the SSL certificate.

NTN

+2
source

Below worked for me:

 Authenticator.setDefault(new MyAuthenticator(" user@account ","password")); ------- public MyAuthenticator(String user, String passwd){ username=user; password=passwd; } 
-one
source

All Articles