1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| package tcpclient
import ( "net" "fmt" "strconv" )
func RequestMsg(serialNum string, randomNum string, toAddr string, fromAddr string) string { body := "<?xml version=\"1.0\"?>\r\n" + "<Config>\r\n" + "<CmdType>ACTIVE</CmdType>\r\n" + "<CmdInfo>GetActiveInfo</CmdInfo>\r\n" + "<SerialNumInfo>\r\n" + "<SerialNum>" + serialNum + "</SerialNum>\r\n" + "</SerialNumInfo>\r\n" + "</Config>"
head := "HIK-PRODUCT\r\n" + "Via:HIKSTOR/1.0/TCP\r\n" + "From:" + fromAddr + "\r\n" + "To:" + toAddr + "\r\n" + "Call-ID:" + randomNum + "\r\n" + "Content-Type:xml\r\n" + "Content-Length:" + strconv.Itoa(len(body)) + "\r\n\r\n" return head + body }
func GetBomInfoFromThirdServ(serialNum string,randomNum string, toAddr string, fromAddr string) (string,error) {
conn,err :=net.Dial("tcp","10.192.44.183:35004") if err != nil {
fmt.Println("dial err",err) return "", err } defer conn.Close() reqmsg := RequestMsg(serialNum,randomNum,toAddr,fromAddr) _, err = conn.Write([]byte(reqmsg)) if err != nil { fmt.Println("send err=", err) return "",err } buf := make([]byte, 1024)
n, err :=conn.Read(buf) if err != nil { fmt.Println("服务器read err=", err) return "",err } return string(buf[:n-1]),nil
}
|