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
精彩评论