dongzhanlu0658 2015-07-20 08:37
浏览 94

http.Client.Do停止执行,没有任何错误

I faced with weird behavior of Do function in http library. In my program I have a worker reading a channel. On each message worker calls a function which makes a http request. Here are the function:

func FetchUrlWithProxy(url string, proxy string) (*http.Response, error) {
    proxyUrl, err := urllib.Parse(proxy) // [1]
    if err != nil {
        return nil, err
    }

    client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
    request, err := http.NewRequest("GET", url, nil)
    if err != nil {
        return nil, err
    }

    log.Println("try ", url) // [2]
    response, err := client.Do(request)
    log.Println("done ", url) // [3]

    if err != nil { // [4]
        return nil, err
    }

    return response, nil
}
  • [1] - it's just net/url imported this this name.
  • [2] - this line is printed eveytime.
  • [3] - this line is never printed without any errors/panics.
  • [4] - so this checking of return value is useless. Execution doesn't reach this point.

This behavior definitely is not what I expected. But if I remove proxy usage and create client as client := &http.Client{} it will work. On other hand the proxy is not broken. I checked it using curl. How can it be?

I will provide any additional information if you need.

  • 写回答

1条回答 默认 最新

  • duanqi5114 2015-07-20 19:45
    关注

    Your code seems to work - I would suspect that there is something happening with the proxy that you are actually using ?

    code :

    package main
    
    import (
        "log"
        "net/http"
        urllib "net/url"
    )
    
    func FetchUrlWithProxy(url string, proxy string) (*http.Response, error) {
        proxyUrl, err := urllib.Parse(proxy) // [1]
        if err != nil {
            return nil, err
        }
    
        client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
        request, err := http.NewRequest("GET", url, nil)
        if err != nil {
            return nil, err
        }
    
        log.Println("try ", url) // [2]
        response, err := client.Do(request)
        log.Println("done ", url) // [3]
    
        if err != nil { // [4]
            return nil, err
        }
    
        return response, nil
    }
    
    func main() {
        res, err := FetchUrlWithProxy("https://www.google.com", "https://www.google.com")
        log.Printf("R %v E %v
    ", res, err)
    }
    

    And the output :

    ➜ go run p.go
    2015/07/20 21:44:45 try  https://www.google.com
    2015/07/20 21:44:46 done  https://www.google.com
    2015/07/20 21:44:46 <nil> Get https://www.google.com: unexpected EOF
    
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图