java http curl,将CURL请求转换为HTTP请求Java

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

解决方案

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值