标签: go-http

什么时候在golang中使用劫持?

我不明白为什么我们使用劫持,因为我可以直接在响应体中写一些东西,有人可以解释一下吗?

func writeSome(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "write some thing")
}
Run Code Online (Sandbox Code Playgroud)

它与此相同:

func hijack(w http.ResponseWriter, r *http.Request) {
    hj, _ := w.(http.Hijacker)
    _, buf, _ := hj.Hijack()
    buf.WriteString("write some thing")
    buf.Flush()
}
Run Code Online (Sandbox Code Playgroud)

我很迷惑

go go-http

15
推荐指数
2
解决办法
7365
查看次数

在同时请求时拨打 tcp I/O 超时

我正在用 Go 构建一个工具,它需要向许多不同的服务器发出大量并发 HTTP 请求。我在 Python 中的初始原型在处理几百个并发请求时没有问题。

但是,我发现在 Go 中Get http://www.google.com: dial tcp 216.58.205.228:80: i/o timeout,如果同时请求的数量超过 ~30-40,这几乎总是会导致某些情况。

我已经在 macOS、openSUSE、不同的硬件、不同的网络和不同的域列表上进行了测试,并且按照其他 Stackoverflow 答案中的描述更改 DNS 服务器也不起作用。

有趣的是,失败的请求甚至不会产生数据包,这在使用 Wireshark 进行检查时可以看出。

有什么我做错了还是 Go 中的错误?

最低可重现程序如下:

package main

import (
    "fmt"
    "net/http"
    "sync"
)

func main() {
    domains := []string{/* large domain list here, eg from https://moz.com/top500 */}

    limiter := make(chan string, 50) // Limits simultaneous requests

    wg := sync.WaitGroup{} // Needed to not prematurely exit before all requests have been finished

    for i, …
Run Code Online (Sandbox Code Playgroud)

network-programming http go go-http

9
推荐指数
1
解决办法
9331
查看次数

使用 HTTP GET 请求拨打 tcp i/o 超时

遇到一些错误,我一定是忽略了一些东西。我该如何调试这个?断开连接?

我读了以下内容:
golang - Why net.DialTimeout get timeout half the time?
去。在服务器程序 golang 中获取错误 i/o 超时
在 ubuntu 14.04 LTS 中获取大量读取 tcp ip:port i/o 超时
在 Go 源代码中定位“read tcp”错误
偶尔出现“http: proxy error: read tcp: i/ Heroku 上的 o 超时”

此处创建错误: https://github.com/golang/go/blob/b115207baf6c2decc3820ada4574ef4e5ad940ec/src/net/net.go#L179

目标:向 url 发送 Get 请求。
预期结果:以 JSON 格式返回正文。
遇到的问题:I/O 超时

它适用于邮递员
编辑:我添加了修改后的超时...

编辑2:跟踪错误

邮递员请求:

GET /v2/XRP-EUR/candles?interval=1h HTTP/1.1
Host: api.bitvavo.com
Run Code Online (Sandbox Code Playgroud)

邮递员结果(1440 行):

[
    [
        1609632000000,
        "0.17795",
        "0.17795",
        "0.17541",
        "0.17592",
        "199399.874013"
    ],
    [
        1609628400000,
        "0.17937",
        "0.18006",
        "0.17622",
        "0.17852",
        "599402.631894"
    ],
    [ …
Run Code Online (Sandbox Code Playgroud)

go go-http

9
推荐指数
2
解决办法
5万
查看次数

你如何使用 Go 1.16 在子文件夹/包中嵌入功能?

Go 1.16 已经发布,我想使用新的嵌入功能。如果一切都在主包中,我可以让它工作。但是不清楚如何处理从子文件夹/包访问资源。尝试通过 embed.FS 支持来实现。

例如,我在处理程序包/文件夹中有一个 main.go 和一个 HTTP 处理程序

如果我将处理程序放在主程序中,它就可以工作。如果我把它放在处理程序包中,它找不到模板。我得到:

handlers/indexHandler.go:11:12: pattern templates: no matching files found exit status 1

同样,如果我从 / 提供它,我可以让它从静态文件夹提供图像。但是我不能同时提供来自 / 的处理程序和来自 / 的静态/图像。如果我将图像放在 /static/ 上,它找不到图像。

我认为这与相对路径有关。但是我无法通过反复试验找到正确的组合......依赖这些功能是否为时过早?

以前我用的是 go-rice,我没有这些问题。但我想尽可能多地使用 std 库。

main.go:

package main

import (...)

//go:embed static
var static embed.FS

func main() {

    fsRoot, _ := fs.Sub(static, "static")
    fsStatic := http.FileServer(http.FS(fsRoot))
    http.Handle("/", fsStatic)
    http.HandleFunc("/index", Index)
    http.ListenAndServe(":8080", nil)
}
Run Code Online (Sandbox Code Playgroud)

处理程序/indexHandler.go:

package handlers

import (...)

//go:embed templates
var templates embed.FS

// Index handler
func Index(w http.ResponseWriter, r …
Run Code Online (Sandbox Code Playgroud)

go go-http

8
推荐指数
1
解决办法
3421
查看次数

去劫持客户端连接

去语言http连接劫持.

我知道如何在服务器端劫持. http://golang.org/pkg/net/http/#example_Hijacker

但有没有办法在客户端劫持它?

go go-http

6
推荐指数
1
解决办法
1753
查看次数

Go 相当于 Python 的 requests.Session,用于使用相同的基本身份验证发出多个请求?

考虑这个在 Go 中使用基本身份验证发出 HTTP 请求的示例:

package main

import (
    "encoding/base64"
    "fmt"
    "io/ioutil"
    "net/http"
    "net/http/httptest"
    "strings"
)

var userName = "myUserName"
var password = "myPassword"

func main() {
    ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        if !checkAuth(w, r) {
            http.Error(w, "You're not authorized!", http.StatusUnauthorized)
            return
        }
        w.Write([]byte("You're authorized!"))
    }))
    defer ts.Close()

    req, err := http.NewRequest("GET", ts.URL, nil)
    check(err)

    req.SetBasicAuth(userName, password+"foo")

    resp, err := http.DefaultClient.Do(req)
    check(err)
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    check(err)

    fmt.Println(string(body))
}

// checkAuth checks authentication (cf. /sf/ask/1535543271/#21937924)
func …
Run Code Online (Sandbox Code Playgroud)

go basic-authentication go-http

6
推荐指数
0
解决办法
3575
查看次数

tls:HTTP 请求没有重新协商错误

我正在尝试在 Go 中发出一个简单的 HTTP 请求,在直接按照指南操作后,我不断收到相同的错误:

local error: tls: no renegotiation

我不太明白如何解释这个?我知道这不是服务器上的问题,因为当我从 python 调用相同的请求时,它返回正常。这是我的代码:

package main

import (
    "fmt"
    "net/http"
    "net/url"
    "strings"
    "time"
)

func main() {
    timeout := time.Duration(20 * time.Second)
    client := &http.Client{
        Timeout: timeout,
    }
    data := url.Values{
        "top":   {"10"},
        "lDate": {"2019-01-01"},
    }
    req, err := http.NewRequest("POST", "https://api.*********.com/AppAvailLoads?", strings.NewReader(data.Encode()))
    if err != nil {
        fmt.Println("Error in construction")
    }
    req.Header.Add("x-cdata-authtoken", "********")
    req.Header.Add("content-type", "application/x-www-form-urlencoded")
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Error in request")
        fmt.Println(err)
    } else { …
Run Code Online (Sandbox Code Playgroud)

go go-http

5
推荐指数
1
解决办法
2941
查看次数

Go http 客户端超时与上下文超时

在 http.Client 中设置的超时和在请求上下文中设置的超时有什么区别?

我见过 2 种在 http 客户端设置超时的方法。

第一的:

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080", nil)
Run Code Online (Sandbox Code Playgroud)

第二:

client := http.Client{
    Timeout: 2 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
    panic(err)
}
Run Code Online (Sandbox Code Playgroud)

什么时候使用一个?

http go go-http

5
推荐指数
1
解决办法
3788
查看次数

net.Listen 和 http.ListenAndServe 功能之间的差异

我是 Go 和网络的新手。我知道 net.Listen 和 http.ListenAndServe 都创建了一个服务器。但它们的功能有什么区别呢?

network-programming go server go-http

4
推荐指数
1
解决办法
2277
查看次数

如何通过 Go 的 MaxBytesReader 确定我是否达到了大小限制

我是 Go 新手,使用 Mux 接受 HTTP POST 数据。我想使用MaxBytesReader来确保客户端不会淹没我的服务器。根据代码,有一个requestBodyLimit布尔值指示是否已达到该限制。

我的问题是:在使用 MaxBytesReader 时,如何确定在处理请求时是否真的达到了最大值?

这是我的代码:

package main

import (
        "fmt"
        "log"
        "html/template"
        "net/http"

        "github.com/gorilla/mux"
)

func main() {
        r := mux.NewRouter()
        r.HandleFunc("/handle", maxBytes(PostHandler)).Methods("POST")
        http.ListenAndServe(":8080", r)
}

// Middleware to enforce the maximum post body size
func maxBytes(f http.HandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
            // As an example, limit post body to 10 bytes
            r.Body = http.MaxBytesReader(w, r.Body, 10)
            f(w, r)
    }
}

func PostHandler(w …
Run Code Online (Sandbox Code Playgroud)

go go-http

4
推荐指数
1
解决办法
5248
查看次数