14

I have the following CURL request Can anyone please confirm me what would 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 be kind enough to help me convert the above curl request completely to httpreq.

Thanks in advance.

Suvi

user2686807
  • 179
  • 1
  • 1
  • 7

3 Answers3

18

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

    }
}
aliteralmind
  • 18,274
  • 16
  • 66
  • 102
Ashay Thorat
  • 620
  • 4
  • 11
  • You can add maven dependency for org.apache.commons.codec.binary.Base64 from here : commons-codec commons-codec 1.9 – kishorer747 Nov 28 '17 at 11:04
3

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

Just in case you must use HttpURLConnection, you can try this links:

You are setting username/password, a HTTP-Header option and ignore SSL certificate validation.

HTH

Community
  • 1
  • 1
BetaRide
  • 14,756
  • 26
  • 84
  • 153
-1

Below worked for me:

Authenticator.setDefault(new MyAuthenticator("user@account","password"));

-------
public MyAuthenticator(String user, String passwd){
    username=user;
    password=passwd;
}
ajgeor
  • 1