有人能告诉我如何使用 go 忽略内容类型吗?我可以用 java 在 Jsoup 中调用 ignoreContentType 方法,但我找不到任何方法可以这样做。希望有人告诉我,谢谢。
Connection.Response response = Jsoup.connect("http://example.com/")
.ignoreContentType(true)
.execute();
System.out.println(response.body());
最佳答案
Java jsoup ignoreContentType
解析响应时是否忽略文档的 Content-Type
。
ResponseWriter
在 Go 中,默认情况下会将 Content-Type 集添加到将写入数据的初始 512 字节传递给 DetectContentType
的结果。
您可以实现自己的 ResponseWriter
,将自己的 Content-Type
设置为 ""
以忽略任何(可能不正确的)从响应数据推导出的内容类型。
func (writer MyOwnResponseWriter) Write(data []byte) (int, error) {
writer.Header().Set("Content-Type", "")
return len(data), nil
}
JimB建议 in the comments只需设置您自己的 Content-Type,如 server.go
包括:
//If the Header does not contain a Content-Type line,
// Write adds a Content-Type set to the result of
// passing the initial 512 bytes of written data toDetectContentType.
关于java - 如何忽略 golang 的内容类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41375662/