拨打一个与比特币的jsonrpc连接

huangapple go评论117阅读模式
英文:

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)

}

英文:
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 following error:
dial tcp user:pass@localhost:8332: too many colons in address user:pass@localhost:8332

How do I authenticate my rpc connection?

答案1

得分: 2

Go的rpc/jsonrpc包(以及更一般的rpc包)不支持身份验证。可以在底层net.Dial函数的第二个参数的文档中找到jsonrpc.Dial的有效字符串。

但我认为你还做了一个很大的假设,即你尝试连接的任何系统(比特币可能?)都支持Go的jsonrpc协议,除非它是用Go编写的,否则几乎肯定不支持。

英文:

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.

答案2

得分: 2

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

英文:
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

huangapple
  • 本文由 发表于 2011年7月6日 22:27:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/6598041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定