开发者

Dialing a jsonrpc connection to bitcoin

package main

import "rpc/jsonrpc"
import "fmt"

func main() {
    rc, e := jsonrpc.Dial("tcp", "user:pass@localhost:8332")
    if e != nil {fmt.Print(e);return;}

    var blocks float64
    rc.Call("getblockcount", "", &blocks)
    if e != nil {fmt.Print(e); return;}
    fmt.Print("%f blocks", blocks)
}

Gives me the followin开发者_运维百科g error: dial tcp user:pass@localhost:8332: too many colons in address user:pass@localhost:8332

How do I authenticate my rpc connection?


The Go rpc/jsonrpc package (and more generally, the rpc package) does not support authentication. A valid string for the jsonrpc.Dial can be found in the documentation for the second argument of the underlying net.Dial function.

But I think you're also making a big assumption that whatever system you're trying to connect to (bitcoin maybe?) supports the Go jsonrpc protocol, which--unless it's written in Go--it almost assuredly does not.


testRequest := `{"jsonrpc": "1.0", "id":"", "method": "help", "params": []}`

request, e := http.NewRequest("POST", brpc.addr, strings.NewReader(testRequest))
request.SetBasicAuth(brpc.user, brpc.pass)

responce, e := brpc.c.Do(request)
// responce.Body has the result
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜