douyan6958 2013-05-31 00:37
浏览 64
已采纳

Go中格式错误的HTTP状态代码“ /”错误

Server.go

package main

import (
    "fmt"
    "net/http"
    //"strings"
   "encoding/json"
   "io/ioutil"
   "strconv"
    "net"
    "bufio"
)

type Message struct {
    Text string
}

func Unmarshal(data []byte, v interface{}) error


func main() {

    //http.HandleFunc("/", handler)
    server,_ := net.Listen("tcp", ":" + strconv.Itoa(8080))
    if server == nil {
        panic("couldn't start listening: ")
    }
    conns := clientConns(server)
    for {
        go handleConn(<-conns)
    }

}


func clientConns(listener net.Listener) chan net.Conn {
    ch := make(chan net.Conn)
    i := 0
    go func() {
        for {
            client, _ := listener.Accept()
            if client == nil {
                fmt.Printf("couldn't accept: ")
                continue
            }
            i++
            fmt.Printf("%d: %v <-> %v
", i, client.LocalAddr(), client.RemoteAddr())
            ch <- client
        }
    }()
    return ch
}


func handleConn(client net.Conn) {
    b := bufio.NewReader(client)
    fmt.Println("Buffer")
    for {
        line, err := b.ReadBytes('
')
        if err != nil { // EOF, or worse
            break
        }
        client.Write(line)
    }
}

Client.go

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"
    "strings"
    "flag"
    //"io"
   // "net"
  //  "net/rpc"
//    "sync"
)

func Unmarshal(data []byte, v interface{}) error

func Marshal(v interface{}) ([]byte, error)


type Message struct {
    Text string
}

func main(){
    var flagtext = flag.String("flagtext", "Hello!", "Flag")
    flag.Parse()
    var text string
    text = *flagtext
    m := Message{text}
    var m1 Message
    b, err := json.Marshal(m)
    if err == nil{
        resp, err := http.Post("http://127.0.0.1:8080","application/json", strings.NewReader(string(b)))
        if err != nil{
            log.Fatal("Error while post: %v",err)
        }
        fmt.Println(resp)

        err = json.Unmarshal(b, &m1)
    }
}

Error I get when I run client.go is this:

Error while post: %vmalformed HTTP status code "/"

Though, the server registers a channel for each post, it shows a malformed HTTP status code. Is it because I'm listening in the wrong channel? I'm confused why this error is occurring.

  • 写回答

2条回答 默认 最新

  • du248227 2013-05-31 01:01
    关注

    This line in the server code:

    client.Write(line)
    

    sends the request line back to the client. Since the client is posting something like GET / HTTP/1.1, this means that the server is responding with something like GET / HTTP/1.1, instead of something like HTTP/1.1 200 OK. The error-message you're seeing is because / appears in the status-code position.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站