向post添加摘要身份验证(java)

nbewdwxp  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(291)

下面的代码在我遇到需要摘要身份验证(用户名admin,密码password)的设备之前运行良好。

public static String excutePost(final String targetURL, final String urlParameters) {
        HttpURLConnection connection = null;
        try {
            final URL url = new URL(targetURL);
            connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            final DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();
            final InputStream is = connection.getInputStream();
            final BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            final StringBuffer response = new StringBuffer();
            String line;
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            return response.toString();
        }
        catch (Exception e) {
            Logger.logFile("HIGH", "ERROR", "ArtNodeDriver", "executePost", e.getMessage());
            e.printStackTrace();
            return null;
        }
        finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

我看过各种摘要认证帖子,它们看起来都非常复杂,有没有一种使用库或其他东西的“更简单的方法”?
我有一个curl命令,效果很好-

Curl --digest -u admin:PASSWORD -X POST -d "group=CPM CPs&optionalItem=CP&optionalItemInstance=1&action=Value 1" http://192.168.150.210/action/status

但是我不知道您是否可以或者应该“在java中运行curl”。
我很感激你。拉尔夫

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题