java远程连接windows服务器

现在项目中需要远程连接 windows的服务器,可以对上面的用户组、用户进行增删改查等操作
但是可以获取到 远程服务器的ip、username、password。但是不能出现远程登录界面。
要求在代码中进行登录。
请教一下各位大神,应该怎么实现?

登录 远程windows 机器,可以使用 mstsc 命令;

通过 mstsc 命令 + 参数 ,应该可以实现 登录成功;

https://blog.csdn.net/skywalkzf/article/details/6462330
https://blog.csdn.net/sinat_31974173/article/details/50214663

https://github.com/liujichengs/java_process
可以参考这个调用外部命令(mstsc )

写一个用于登录的方法;因为我这个返回值是jsp 所以就只取了Set-Cookie作为判断
登录专用方法

    public static String loginPost(String url, Map<String, String> param) throws Exception {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建参数列表
            if (param != null) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : param.keySet()) {
                    paramList.add(new BasicNameValuePair(key, param.get(key)));
                }
                // 模拟表单
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");
                httpPost.setEntity(entity);
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            Header[] headers = response.getAllHeaders();
            logger.info("Header"+(headers==null?"0":headers.length));
            if (headers==null) return "";
            for (Header header : headers) {
                logger.info("登录请求;name:"+header.getName()+";value:"+header.getValue());
                if ("Set-Cookie".equals(header.getName()))
                    return header.getValue().split(";")[0];
            }
            return "";
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (response != null)
                response.close();
        }
        return resultString;
    }

登录方法

private boolean login() throws Exception {
        String pwd = "pwd";
        pwd = "加密操作"
        pwd = pwd.toLowerCase();
        String url = *** + "/login";
                //登录参数赋值
        Map<String, String> map = new HashMap<>();
        map.put("lang", "********");
        map.put("password", pwd);
        map.put("account", "*******");
        map.put("pwd", "******");
        //调用请求
        String cook = HttpClientUtil.loginPost(url, map);
        if ("".equals(cook)){
            logger.info("登录设备失败!");
            return false;
        }
        cook = cook.split(";")[0];
        cookie = cook;
        return true;
    }

或者像这样 将参数对象转成JSON字符串

public static String doPostJson(String url, String json, String cookie) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            if (StringUtils.isNotEmpty(cookie))
                httpPost.setHeader("Cookie", cookie);
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (response != null)
                response.close();
        }
        return resultString;
    }

或者这样 模拟表单

public static String doPost(String url, Map<String, Object> param, String cookie) throws Exception {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建参数列表
            if (param != null) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : param.keySet()) {
                    paramList.add(new BasicNameValuePair(key, param.get(key).toString()));
                }
                // 模拟表单
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");
                httpPost.setEntity(entity);
                if (StringUtils.isNotEmpty(cookie))
                    httpPost.setHeader("Cookie", cookie);
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (response != null)
                response.close();
        }
        return resultString;
    }

resultString就是请求返回的参数,需要手动转成所需对象类型

用alibaba 的JSON转换方法
import com.alibaba.fastjson.JSONObject;

数组对象转换
List<T> datas = JSONObject.parseArray("JSON字符串", T.class);

对象转换:T t = JSONObject.parseObject("JSON字符串", T.class);