将CURL请求转换为HTTP请求Java [英] Converting CURL request to HTTP Request Java

查看:3066
本文介绍了将CURL请求转换为HTTP请求Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下CURL请求任何人都可以确认我是什么是subesquest HTTP请求

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

会是这样吗?

    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请求

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

提前感谢。

Suvi

推荐答案

有很多方法可以实现这一点。下面一个在我看来是最简单的,同意它不是很灵活,但工作。

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

    }
}

这篇关于将CURL请求转换为HTTP请求Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆