请教大家一个关于net/http的问题(已解决)

nolove · · 2159 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

谢谢大家 我有一段代码,就是发送post数据包的,如果我将目标的url写死,程序就正常运行,如果将目标的url写成 flag.StringVar就会报错 + 报错的代码如下: ``` package main import ( "flag" "net/http" "strings" "fmt" "io/ioutil" ) func httpDo(url string,name string) { ip := strings.Replace(url, "https://", "", -1) ip = strings.Replace(ip, "http://", "", -1) client := &http.Client{} password := "WEBVAR_USERNAME=" + name + "&WEBVAR_PASSWORD=" + name req, err := http.NewRequest("POST", url+"/rpc/WEBSES/create.asp", strings.NewReader(password)) if err != nil { fmt.Println("主机可能不存活") } req.Header.Set("Accept", "application/json, text/plain, */*") req.Header.Set("Origin", url) req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36") req.Header.Set("Content-Type", "application/json;charset=UTF-8") req.Header.Set("Referer", url+"/index.html") req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9") req.Header.Set("Cookie", "BMC_IP_ADDR="+ip+"; test=1") resp, err := client.Do(req) if err != nil{ fmt.Println("主机可能不存活") } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("读取失败") } str := string(body) if strings.Contains(str,"HAPI_STATUS:0"){ fmt.Println("破解成功---帐号:"+name+"密码:"+name) }else{ fmt.Println("破解失败") } } func main() { var url string flag.StringVar(&url,"u","","http(s)://url") //go httpDo(url,"admin") httpDo(url,"root") } ``` + 报错内容 ``` panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x644f60] goroutine 1 [running]: main.httpDo(0x0, 0x0, 0x6da4f0, 0x4) /root/go/src/inspur_pass/inspurtest.go:31 +0x970 main.main() /root/go/src/inspur_pass/inspurtest.go:48 +0xb6 exit status 2 ``` + 写死url,就不报错,代码如下: ``` package main import ( "net/http" "strings" "fmt" ) func httpDo() { url := "http://www.baidu.com" ip := strings.Replace(url, "https://", "", -1) ip = strings.Replace(ip, "http://", "", -1) name := "admin" client := &http.Client{} password := "WEBVAR_USERNAME=" + name + "&WEBVAR_PASSWORD=" + name req, err := http.NewRequest("POST", url+"/rpc/WEBSES/create.asp", strings.NewReader(password)) if err != nil { // handle error } req.Header.Set("Accept", "application/json, text/plain, */*") req.Header.Set("Origin", url) req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36") req.Header.Set("Content-Type", "application/json;charset=UTF-8") req.Header.Set("Referer", url+"/index.html") req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9") req.Header.Set("Cookie", "BMC_IP_ADDR="+ip+"; test=1") //req.Header.Set("Content-Type", "application/x-www-form-urlencoded") //req.Header.Set("Cookie", "name=anny") resp, err := client.Do(req) defer resp.Body.Close() /* body, err := ioutil.ReadAll(resp.Body) if err != nil { // handle error } */ fmt.Println(resp.StatusCode) //fmt.Println(string(body)) } func main() { // httpGet() // httpPost() // httpPostForm() httpDo() } ``` 请问应该如何解决这个问题,又是什么原因导致的这个问题呢? 再次感谢大家 + 解决了,缺少 flag.Parse()

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2159 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传