Center.java 文件源码

java
阅读 28 收藏 0 点赞 0 评论 0

项目:hermes-java 作者:
/**
 * 请求服务
 *
 * @param serverId 服务ID
 * @param name     方法名
 * @param data     数据
 * @return 响应数据
 * @throws Exception 加解密以及io错误
 */
public String call(String serverId, String name, String data) throws Exception {
    // 加密请求数据
    byte[] in = data.getBytes("utf-8");
    if (in.length > this.length - Constant.RSA_RESERVED_LENGTH) {
        throw new Exception("request data is too big");
    }
    byte[] bytes = RsaUtils.encryptByPublicKey(in, this.publicKey);
    // bytes转16进制发送
    String resp = HttpUtils.sendGet(String.format("http://%s/server?sessionId=%s&serverId=%s&name=%s&data=%s",
            this.host, this.sessionId, serverId, name, CoderUtils.bytesToHex(bytes)));
    JSONObject obj = JSON.parseObject(resp);
    Integer code = obj.getInteger(Constant.CODE);
    if (code != 0) {
        throw new Exception(String.format("call server fail, fail code: %d", code));
    }
    // 解密响应
    byte[] receive = RsaUtils.decryptByPublicKey(obj.getBytes(Constant.DATA), this.publicKey);
    return new String(receive);
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号