通过HttpURLConnection将curl转换为java?

时间:2017-10-04 07:36:54

标签: java curl httpurlconnection

如何将以下curl命令编码为java,

curl -k --request POST "<hostname>/login" -d IDToken1="user" -d IDToken2="password" --cookie-jar cookie.txt

curl -w "%{http_code}" -G -s --insecure --request GET --cookie cookie.txt '<requested-url>'

我能够执行帖子但不确定如何在cookie中保留响应然后,我如何在HTTP get请求中使用cookie。

在发帖时我正在尝试的是

public void httpPostRequest() throws Exception{
        URL url = new URL("url/login");
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
        String userCredentials = "user"+":"+"password";
        String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
        urlConnection.setRequestProperty ("Authorization", basicAuth);
        try {
            urlConnection.setRequestMethod("POST");
        } catch (ProtocolException e) {
            e.printStackTrace();
        }
        cookies = urlConnection.getHeaderFields().get("Set-Cookie");
        System.out.println("these are cookies"+cookies);
}

我做错了因为cookies列表是空的

0 个答案:

没有答案