Mac*_*exa -1 http go http-headers
我需要使用 Go HTTP 客户端访问响应历史标头。我不知道如何使用net/http. 在python3中,它是由库完成response.history的requests。我的请求是这样的:308 -> 302 -> 200。我需要 302 的响应标头(但该标头可以出现在任何像 308 或 200 这样的响应中,所以我需要迭代)。那么,我该怎么做呢?为了您的回答,只需抓住special标题即可。要重现我的情况,您必须首先创建 3 个文件,index.php、test.php 和 php.php,并在 localhost 中托管一个服务器。
index.php 内容:
<?php
header("Special: Header");
header("Location: http://127.0.0.1/php.php");
?>
Run Code Online (Sandbox Code Playgroud)
php.php 内容:
<?php
echo "<html><head><title>Test Site</title></head><body>Hello World</body></html>";
?>
Run Code Online (Sandbox Code Playgroud)
test.php 内容:
<?php
header("Location: http://127.0.0.1/index.php")
?>
Run Code Online (Sandbox Code Playgroud)
主机服务器在php -S 0.0.0.0:80,special使用 go捕获标头。使用 python3 我可以这样做:
<?php
header("Special: Header");
header("Location: http://127.0.0.1/php.php");
?>
Run Code Online (Sandbox Code Playgroud)
或者
<?php
echo "<html><head><title>Test Site</title></head><body>Hello World</body></html>";
?>
Run Code Online (Sandbox Code Playgroud)
我很难在 go 中重现它。虽然第一个响应不是 308 重定向,但我试图使其尽可能相似。
HTTP/1.1 302 Found
Host: 127.0.0.1
Date: Fri, 20 Nov 2020 14:04:11 GMT
Connection: close
X-Powered-By: PHP/7.4.11
Location: http://127.0.0.1/index.php
Content-type: text/html; charset=UTF-8
HTTP/1.1 302 Found
Host: 127.0.0.1
Date: Fri, 20 Nov 2020 14:04:11 GMT
Connection: close
X-Powered-By: PHP/7.4.11
Special: Header
Location: http://127.0.0.1/php.php
Content-type: text/html; charset=UTF-8
HTTP/1.1 200 OK
Host: 127.0.0.1
Date: Fri, 20 Nov 2020 14:04:11 GMT
Connection: close
X-Powered-By: PHP/7.4.11
Content-type: text/html; charset=UTF-8
Run Code Online (Sandbox Code Playgroud)
这个想法是迭代所有标题,如果找到该标题,则更改问题的代码流。在 Go 中可以吗?我想将我的 python 代码移动到 golang,这是我在 python3 中的实现:https://pastebin.com/3DRcUaxj
假设resp是*http.Responsehttp客户端返回的,使用如下代码查找header:
for r := resp; r != nil; r = r.Request.Response {
if s := r.Header.Get("Special"); s != "" {
// found the special header
break
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
78 次 |
| 最近记录: |