dongzhiyi2006 2013-07-23 22:34
浏览 184
已采纳

Golang http get请求在部分但不是全部URL上中断

Right now I'm fetching urls from indiegogo as part of a side project using the basic get request template found [here][1]. I then translate the byte data into a string using

responseText, err:= ioutil.ReadAll(response.Body)
trueText:= string(responseText)

with appropriate error handling where needed

It works fine for repeated attempts at getting and some other urls of varying length(at least as large as the previous url and some longer than the next).

Strangely, when I attempt to get it breaks and throws a runtime error of

panic: runtime error: index out of range

and exits with a status of 2. I'm curious as to what the issue could be.

I know it isn't indiegogo getting angry about my once a minute requests and cutting my connection because I can request continiously for 20 minutes at with no issue. Give it a bit of downtime and it still completely breaks on

Thanks for the assistance

EDIT, it appears as though it was a malformed bit of html in some of the pages that messed with a loop I was running based on the content that managed to break go in the runtime on only some urls. Thanks for the help

[1]:

  • 写回答

1条回答 默认 最新

  • duancheng7743 2013-07-24 01:29
    关注

    There is no error when getting from the url and converting the body to the Go string type. For example,

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "log"
        "net/http"
    )
    
    func main() {
        url := "http://www.indiegogo.com/projects/culcharge-smallest-usb-charge-and-data-cable-for-iphone-and-android"
        res, err := http.Get(url)
        if err != nil {
            log.Fatal(err)
        }
        body, err := ioutil.ReadAll(res.Body)
        res.Body.Close()
        if err != nil {
            log.Fatal(err)
        }
        text := string(body)
        fmt.Println(len(body), len(text))
    }
    

    Output:

    66363 66363
    

    You didn't provide us with a small fragment of code which compiles, runs, and fails in the manner you describe. That leaves us all guessing.

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

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境