2016-12-29 58 views
-1

有人可以告诉我如何忽略内容类型与去?我可以用java调用Jsoup中的ignoreContentType方法,但是我找不到任何方法可以这样做。希望有人会告诉我,谢谢。如何忽略golang的内容类型?

Connection.Response response = Jsoup.connect("http://example.com/") 
      .ignoreContentType(true) 
      .execute(); 
System.out.println(response.body()); 
+0

你为什么试图忽略内容类型? Content-Type是否导致您在代码中出现问题?如果是的话,你得到的错误是什么? – 2016-12-29 08:52:54

+0

没有错误返回,我会发表另一条评论来解释。 – henghanan

回答

0

我已经解决了我的问题,阅读Jsoup的源代码后,只需设置响应头

谢谢你们。

0

的Java jsoup ignoreContentType有解析响应时忽略文档的Content-Type

Go中的ResponseWriter默认会将最初512字节的写入数据传递给DetectContentType的结果添加一个Content-Type集合。

您可以实现自己的ResponseWriter,你设定自己的Content-Type""以忽略响应的数据推导出任何(可能是不正确的)内容类型。

func (writer MyOwnResponseWriter) Write(data []byte) (int, error) { 
    writer.Header().Set("Content-Type", "") 
    return len(data), nil 
} 

JimB建议in the comments简单地设置自己的内容类型,为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. 
+0

您也可以设置Content-Type标头 - go http服务器不会尝试检测并覆盖它,如果它已经存在。 – JimB

+0

@JimB我会认为去http服务器可以做出自己的响应,其自己的头取决于如何实现http服务器。 – VonC

+0

我不确定遵循的是什么,但是在'ReponseWriter'中设置的头部优先,直到调用'WriteHeader'。如果你在server.go源文件中查找'DetectContentType',你可以看到它首先检查是否已经设置了Content-Type头。 – JimB