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.

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

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型