java - JSON RPC Java dzhuvinov - 如何设置用户名和密码?

标签 java json http rpc basic-authentication

我正在使用来自 dzhuvinov 的 JSON-RPC 2.0 的 Java 库.我在为 basic access authenticaition 设置用户名和密码时遇到问题电话。我的代码如下所示:

public static void main(String[] args)
{
    URL serverURL = null;
    try {
        serverURL = new URL("http://user:pass@127.0.0.1:18332/");

    } catch (MalformedURLException e) {
        System.err.println(e.getMessage());
        return;
    }
     JSONRPC2Session mySession = new JSONRPC2Session(serverURL);
     String method = "getinfo";
     int requestID = 0;
     JSONRPC2Request request = new JSONRPC2Request(method, requestID);
     JSONRPC2Response response = null;
     try {
             response = mySession.send(request);

     } catch (JSONRPC2SessionException e) {
             System.err.println(e.getMessage());
             return;
     }
     if (response.indicatesSuccess())
        System.out.println(response.getResult());
    else
        System.out.println(response.getError().getMessage());
}

我得到的回应是:

Network exception: Server returned HTTP response code: 401 for URL: http://user:pass@127.0.0.1:18332/

在 Python 中尝试类似的代码,我得到了正确的结果:

access = jsonrpc.ServiceProxy("http://user:pass@127.0.0.1:18332/")
print access.getinfo()

如何为我的 JSON RPC 调用配置基本访问身份验证?

最佳答案

final String rpcuser ="...";
final String rpcpassword ="...";

Authenticator.setDefault(new Authenticator() {
  protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication (rpcuser, rpcpassword.toCharArray());
  }});

关于java - JSON RPC Java dzhuvinov - 如何设置用户名和密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14788305/

相关文章:

java - 异步任务使我的应用程序崩溃

java - 从 JDBC MSSQL 获取返回值

java - spring 如何知道要使用哪个 View 解析器?

javascript - 如何查询 json 的值?

php - 查找 json 数组中的对象数

c# - HttpListenerResponse OutputStream.Write 在我的服务器上非常慢

java - Eclipse:未设置 JAVA_HOME

java - 如何在Java中将group-by对象列表转换为html无序列表?

json - Apache Hive : Convert Map<string, string> 到 json 字符串?

amazon-web-services - 如何在 AWS Application Load Balancer 中限制文件上传大小?