我最近一直在处理 HTTP 标头。我正在根据 RFC 规定的冒号分隔从 HTTP 标头请求中解析字段和值。在蟒蛇中:
header_request_line.split(":")
Run Code Online (Sandbox Code Playgroud)
但是,如果值字段中允许使用冒号,则会出现问题。考虑:
User-Agent: Mozilla:4.0
Run Code Online (Sandbox Code Playgroud)
这将被分成 3 个字符串,而不是我想要的 2 个。
我正在从 RESTful Web 服务中获取资源,并在响应中添加以下两个标头:
Cache-Control: public, max-age=120
ETag: "3D03F8066015A1438532E049AE5E16EA"
Run Code Online (Sandbox Code Playgroud)
浏览器第一次收到这个响应时,它正确地缓存了 2 分钟。
但是,下次我请求资源时,我的浏览器现在发送了一个IfNoneMatch标头,并按304 Not Modified预期获得了一个标头,以及另一个Cache-Control要缓存 2 分钟的标头。
但是浏览器没有缓存响应。我不断刷新页面,它不断提出请求,因此不断获得304 Not Modified.
这是正常行为吗?
我如何告诉浏览器在获得 2 分钟后重新验证其缓存资源304 Not Modified,并且不再发出任何请求?
etag caching http-headers http-status-code-304 if-none-match
我在 android 中调用 web 服务。因为我想调用 URL 我没有向服务器发送任何参数,只是调用 URL,
但它得到错误如 [10520] BasicNetwork.performRequest: Unexpected response code 401
我的代码是
RequestQueue queue = Volley.newRequestQueue(getActivity());
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, Server.URL, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
// display response
hideProgressDialog();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
hideProgressDialog();
}
}
);
// add it to the RequestQueue
queue.add(getRequest);
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
根据 RFC http://tools.ietf.org/html/rfc6265#page-7允许有两个具有相同“Set-Cookie”键的标头。RFC 中提供的示例是 -
??Set-Cookie: SID=31d4d96e407aad42; Path=/; Secure; HttpOnly
Set-Cookie: ??lang=en-US; Path=/; Domain=example.com
Run Code Online (Sandbox Code Playgroud)
如何?做什么?我用 Jetty(或任何其他 servlet 容器)实现了相同的目标?当我以这种方式调用 httpServletResponse.addHeader 时-
?httpServletResponse.addHeader("Set-Cookie", "SID=31d4d96e407aad42; Path=/; Secure; HttpOnly");
httpServletResponse.addHeader("Set-Cookie", "lang=en-US; Path=/; Domain=example.com");?
Run Code Online (Sandbox Code Playgroud)
我看到第二个 addHeader() 没有添加新标题。根据此方法的javadoc-
添加具有给定名称和值的响应标头。此方法允许响应标头具有多个值。
因此,似乎有多个 allowed 值,但我不确定如何在 servlet 响应中使用多个“Set-Cookie”。
我目前正在编写一个 HTTP 客户端来对返回 HTTP 响应的 URL 执行 HTTP POST。
但是,对于错误消息代码 400 和 500,它发送回非分块 HTTP 响应,对于成功消息 201,它发送分块响应。
在请求中,我设置了内容长度,所以我不确定为什么它仍然向我们发送分块传输编码。我可以在请求中设置任何其他标头,它会告诉 HTTP 服务器不要发送分块编码吗?
headerList.append("POST /v2/charges HTTP/1.1")
headerList.append("Content-Type: application/json")
headerList.append("host: xxxxxxxxx")
headerList.append("request-id: ABCD001123")
headerList.append("Content-length: %d" %len(Msg))
hostReqHeader = "\r\n".join(headerList)
reqData = hostReqHeader + '\r\n\r\n' + qbPosMsg
Run Code Online (Sandbox Code Playgroud)
我使用套接字来发送这些 HTTP 消息,而不是使用 httplib 或请求库。
这样对吗?
var headers = new Headers();
headers.append('Accept', 'application/json, application/pdf')
Run Code Online (Sandbox Code Playgroud)
我想同时接受 json 和 pdf 文件。
我正在尝试使用 phpunit 测试我在 laravel 5 中编写的 Web 服务。我是这个测试框架的新手。应用程序使用 JWT 进行身份验证,该身份验证在请求的标头中传递。
我的测试看起来像这样:
$response = $this->call('POST', 'authenticate', [
'username' => 'carparts',
'email' => 'admin@admin.com',
'password' => 'password'
]);
$token = 'Bearer ' . json_decode($response->getContent())->token;
$response = $this->call('POST', 'users', [
"first_name" => "Test",
"last_name" => "Test",
"email" => 'test@test.com',
"password" => "testing",
"role" => "1"
], [], [], [
'Authorization' => $token
]);
dd($response->getContent());
Run Code Online (Sandbox Code Playgroud)
令牌返回正常,但是当我尝试在下一个请求中使用以创建新用户时,它失败说无法从请求中解析令牌。当我request()->headers在中间件中做检查时,即使是这样也不显示标题。我究竟做错了什么?如何使用 PHPUnit 在请求中传递标头?
我已经使用 ETag 有一段时间了,我对如何使用它们以及它们允许做什么感到满意。
但是,我有时会遇到 ETag 验证请求。在我的服务器上,我可以看到我的浏览器访问了资源 URL,然后得到了“未更改”响应。但是,它仍然是一个请求。
一个聪明的网络服务器(我在 NodeJS 中编写我的代码)如何避免长时间受到验证的影响cache-control: max-age=N?
例如,mymax-age表示 TTL 为 30 天。客户根本不应该在那个时候验证 Etag。那可能吗?
curl -L -i google.com
Run Code Online (Sandbox Code Playgroud)
我想将响应中的 HEADER 和 CONTENT 拆分为两个变量
curl -I google.com
curl -L google.com
Run Code Online (Sandbox Code Playgroud)
我不能使用这两个,因为我打算将它与 10000 多个链接一起使用
Header 和 Content 都可以有三个或更多的空行,所以每次都不能拆分空行
我找到了答案
b=$(curl -LsD h google.com)
h=$(<h)
echo "$h$b"
Run Code Online (Sandbox Code Playgroud)
这段代码也有效
curl -sLi google.com |
awk -v bl=1 'bl{bl=0; h=($0 ~ /HTTP\/1/)} /^\r?$/{bl=1} {print $0>(h?"header":"body")}'
header=$(<header)
body=$(<body)
Run Code Online (Sandbox Code Playgroud) 我想在我的 HTTP 标头中发送 json 数据。
我使用的是 Codeigniter PHP,所以我在我的控制器中做了这个:
header('Content-Type: application/json');'
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
$request = array(
'request' => $_GET['request'],
'device_id' => $_GET['device_id'],
'launch_date'=> $_GET['launch_date'],
'allowed_hours'=>$_GET['allowed_hours'],
'site_id'=>$_GET['site_id'],
'product'=>$_GET['product'],
'software_version'=>$_GET['software_version'],
'platform_os'=>$_GET['platform_os'],
'platform'=>$_GET['platform'],
'platform_model'=>$_GET['platform_model']
);
$response = array(
'response_code' =>200 ,
'device_id'=>$_GET['device_id'],
'allowed_hours'=>$_GET['allowed_hours'],
'product'=>'mlc',
'prov_ur'=>NULL
);
header('Content-Type: application/json');
echo json_encode( $response );
Run Code Online (Sandbox Code Playgroud)
但是当我打印我的标题时,我得到
遇到 PHP 错误严重性:注意
消息:未定义索引:请求
文件名:admin/license.php
行号:22
遇到 PHP 错误严重性:注意
消息:未定义索引:allowed_hours
文件名:admin/license.php
行号:25
遇到 PHP 错误严重性:注意
消息:未定义索引:allowed_hours
文件名:admin/license.php
行号:40
{"response_code":200,"device_id":"70D0D01FBAD2","allowed_hours":null,"product":"mlc","prov_ur":null}array(10) { ["Host"]=> string(14) ) "192.168.50.123" ["Connection"]=>
string(10) "keep-alive" ["Cache-Control"]=> string(9) …
http-headers ×10
http ×3
etag ×2
php ×2
android ×1
bash ×1
caching ×1
codeigniter ×1
curl ×1
express ×1
http-caching ×1
java ×1
javascript ×1
jetty ×1
linux ×1
node.js ×1
phpunit ×1
phpwebsocket ×1
python ×1
rfc ×1
servlets ×1
shell ×1
testing ×1
tomcat ×1
validation ×1