duanpanyang1962 2016-10-08 12:13
浏览 49
已采纳

无法使用GO中的网络库执行简单的HTTP POST。 我究竟做错了什么?

I'm trying to do a simple RAW data UPLOAD using the net library (not the net/http). I have a simple php script that just spits out whatever was sent to it. The problem is that the php script doesn't receive anything. What am I doing wrong?

conn, err := net.Dial("tcp", "127.0.0.1:80" )
fmt.Fprintf(conn, "POST /handleupload.php HTTP/1.0

")
n, err := conn.Write([]byte("ABCDEFGHIJ"))
status, err := bufio.NewReader(conn).ReadString('z')
fmt.Println( status )
  • 写回答

1条回答 默认 最新

  • dongyi2534 2016-10-08 12:39
    关注

    Because the Content-Length header is absent, the server is waiting to read to EOF before completing the request. Try this:

    conn, err := net.Dial("tcp", "127.0.0.1:80" )
    fmt.Fprintf(conn, "POST /handleupload.php HTTP/1.0
    
    ")
    n, err := conn.Write([]byte("ABCDEFGHIJ"))
    if c, ok := conn.(*net.TCPConn); ok {
        c.CloseWrite()
    }
    status, err := bufio.NewReader(conn).ReadString('z')
    fmt.Println( status )
    

    Another option is to write a Content-Length request header.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序