Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting CURL request to HTTP Request Java

Tags:

java

curl

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

like image 788
user2686807 Avatar asked Sep 05 '13 12:09

user2686807


People also ask

How do I change my curl to request?

Convert Curl command to the HTTP request using ReqBin. Enter your Curl request, click the Submit button to check if you entered the Curl command correctly, and then switch to the Raw tab to see the generated HTTP request. You can also convert Curl requests to PHP, Python, JavaScript, and C # code.

Does curl work on HTTP?

cURL supports several different protocols, including HTTP and HTTPS, and runs on almost every platform.

What HTTP method does curl use?

curl knows the HTTP method If you just pass in a HTTP URL like curl http://example.com , curl will use GET. If you use -d or -F curl will use POST, -I will cause a HEAD and -T will make it a PUT.


2 Answers

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

    }
}
like image 138
Ashay Thorat Avatar answered Sep 21 '22 15:09

Ashay Thorat


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.

  • How to set username/password check this thread: Connecting to remote URL which requires authentication using Java
  • How to set eader options have a look at this thread: How to modify the header of a HttpUrlConnection
  • How to ignore SSL certificate validation is described here http://www.obsidianscheduler.com/blog/ignoring-self-signed-certificates-in-java .

HTH

like image 44
BetaRide Avatar answered Sep 19 '22 15:09

BetaRide