简体   繁体   English

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

[英]http.Client.Do stops execution without any errors

I faced with weird behavior of Do function in http library. 我在http库中遇到了Do函数的怪异行为。 In my program I have a worker reading a channel. 在我的程序中,有一个工人正在阅读频道。 On each message worker calls a function which makes a http request. 在每个消息上,工作者调用一个发出http请求的函数。 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. [1] -只是net/url导入了此名称。
  • [2] - this line is printed eveytime. [2] -此行显示eveytime。
  • [3] - this line is never printed without any errors/panics. [3] -此行绝不会出现任何错误/惊慌。
  • [4] - so this checking of return value is useless. [4] -这样对返回值的检查是无用的。 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. 但是,如果我删除代理使用情况并将client client := &http.Client{}创建为client := &http.Client{}它将可以正常工作。 On other hand the proxy is not broken. 另一方面,代理没有损坏。 I checked it using curl . 我用curl检查了一下。 How can it be? 怎么可能?

I will provide any additional information if you need. 如果您需要,我会提供任何其他信息。

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\n", 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM