我正在尝试向运行PHP应用程序的本地主机端口8080发送POST请求。
Curl工作正常:
curl --data "key=asdf" http://localhost:8080/
但是在Go中我会遇到以下错误:
Post http://localhost:8080: dial tcp 127.0.0.1:8080: connection refused
这里是代码:
func submitForm(value string){
resp, err := http.PostForm("http://localhost:8080", url.Values{"key": {value}})
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println("status = ",resp.Status)
}
submitForm("asdf")
我使用httpry监控了http流量,发现没有生成任何http请求,但仍有一些数据包:
4 packets received, 0 packets dropped, 0 http packets parsed
更多的事实:
- 我的操作系统是Linux
我使用PHP内置的Web服务器。 服务器是使用以下命令启动的:
php -S localhost:8080
submitForm
函数以及如何启动 PHP 服务器?服务器和这段 Go 代码在同一台机器上运行,是吗? - Ethan