dongshuofu0039 2018-07-09 04:17
浏览 25
已采纳

回应:身体返回“空”身

I am getting a response back as well as header responses and everything, but for some reason the body is empty:

https://repl.it/repls/HastyAggravatingArchitect

package main

import (
    "fmt"
    "net/http"
    "io/ioutil"
)


const url = "http://comicbookdb.com/search.php"

func main() {
    request, err := http.NewRequest(http.MethodGet, url, nil)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    q := request.URL.Query()
    q.Add("form_search", "Captain America")
    q.Add("form_searchtype", "Character")
  // http://comicbookdb.com/search.php?form_search=captain%20america&form_searchtype=Character
    request.URL.RawQuery = q.Encode()
    client := http.DefaultClient
    response, err := client.Do(request)
    fmt.Println(response.Header.Get("Date"))
    fmt.Println(response.StatusCode)
    fmt.Println(response.Header.Get("Server"))
    fmt.Println(response.Body)  

    r, err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Println(err.Error())
    }
    fmt.Println(string(r))
}

I commented in the full URL so that you can check it out yourself and see that the response body should not be empty.

  • 写回答

2条回答 默认 最新

  • douchu2823 2018-07-09 06:14
    关注

    The problem is not in the go program, you need provide additional header in this case it's a Cookie header:

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "net/http"
    )
    
    const url = "http://comicbookdb.com/search.php"
    
    func main() {
        request, err := http.NewRequest(http.MethodGet, url, nil)
        if err != nil {
            fmt.Println(err.Error())
            return
        }
        // Here is requeired header
        request.Header.Add("Cookie", "PHPSESSID=jmujtqjctuk1bv1g02ni88q9u5'") 
        q := request.URL.Query()
        q.Add("form_search", "Captain America")
        q.Add("form_searchtype", "Character")
        request.URL.RawQuery = q.Encode()
        client := &http.Client{}
        response, err := client.Do(request)
        if err != nil {
            fmt.Println(err.Error())
            return
        }
        fmt.Println(response.Header.Get("Date"))
        fmt.Println(response.StatusCode)
        fmt.Println(response.Header.Get("Server"))
        fmt.Println(response.Body)
    
        r, err := ioutil.ReadAll(response.Body)
        if err != nil {
            fmt.Println(err.Error())
        }
        fmt.Println(string(r))
    }
    

    The output:

    Mon, 09 Jul 2018 06:13:35 GMT
    200
    Apache
    &{0xc420060040 {0 0} false <nil> 0x5e2200 0x5e2190}
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org ...
    ..... omitted ....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?