当我们输入一个 url 并将其从浏览器发送到 http 服务器时,我们会从服务器收到响应,有时一个 url 会有多个 http 响应的请求,有没有一种方法可以知道哪个 http 响应包是最终的?
我经常python twisted查看响应标头,但无法弄清楚第一个标头和最终标头的区别。
我在使用 FPDF 库生成 pdf 文件时得到此输出。
\n\n\n%PDF-1.3 3 0 obj <> endobj 4 0 obj <> 流\nx\xef\xbf\xbdU\xef\xbf\xbd\xef\xbf\xbdn\xef\xbf\xbd0\xef\xbf\xbd\xef \xbf\xbd<\xef\xbf\xbd\xcb\x81\xef\xbf\xbd7\xef\xbf\xbd\xef\xbf\xbd8\'\xef\xbf\xbd!Z\xef\xbf\xbd\ xef\xbf\xbd\xef\xbf\xbdq(U~\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd!B\xef\xbf\xbd8\xef\xbf\xbd\xef\xbf \xbdo\xef\xbf\xbde\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdl\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbde \xef\xbf\xbd&\xef\xbf\xbd\xef\xbf\xbdl\xef\xbf\xbdt\xca\x99\xef\xbf\xbd\xef\xbf\xbd:Cl\xef\xbf\xbdk|| \xef\xbf\xbd\xef\xbf\xbdp\xef\xbf\xbd|K\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbde\xef\xbf\xbd \'\xef\xbf\xbd-9\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdB\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdWj\xef\xbf \xbd$F\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdV\xef\xbf\xbd\xef\xbf\xbdt\xef\xbf \xbd\xef\xbf\xbd\xef\xbf\xbdq\xef\xbf\xbd\xef\xbf\xbd3to\xef\xbf\xbd\xef\xbf\xbdXrlQP\xef\xbf\xbd%\xef\xbf\ xbd\xef\xbf\xbd\xef\xbf\xbdn-c\xef\xbf\xbdX\xef\xbf\xbd\xef\xbf\xbdB_!Sl\xef\xbf\xbd\xef\xbf\xbd\xef\ xbf\xbd\xef\xbf\xbd\xef\xbf\xbd+\xef\xbf\xbd\xef\xbf\xbd\n \xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\ xbf\xbdB\xef\xbf\xbd)\xd7\xb0\xef\xbf\xbdI\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd(m\xef\xbf \xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdHV 端流 endobj 1 0 obj <> endobj 5 0 obj\n <> endobj 2 0 obj << /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI]\n /Font << /F1 5 0 R >> /XObject << >> >> endobj 6 0 obj << /Producer\n (FPDF 1.7) /CreationDate (D :20151013130538) >> endobj 7 0 …
我只想缓存某些呼叫而不是全部。这是我现在拥有的代码,但它将影响所有改造调用:
int cacheSize = 10 * 1024 * 1024; // 10 MB
Cache cache = new Cache(getCacheDir(), cacheSize);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cache(cache)
.build();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://10.0.2.2:3000/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
Run Code Online (Sandbox Code Playgroud)
当我只想为该调用进行缓存时,是否可以在标头中放入一些内容?
例如:
// 我想我足以在标题中添加一些内容。我只想缓存客户端(android)端的调用。所以我想改造可以记住响应并缓存它以供下一次调用,但我不希望它用于我的所有调用,只需要我想要的那些,也许1或2。其余的可以一直连接到服务器。这是如何实现的?
@Headers("Cache-Control:????XXXXX) //is it possible this way ?, how ?
@GET("getBusiness.action")// Store information
Call<RestaurantInfoModel> getRestaurantInfo(@Query("userId") String userId,@Query("businessId") String businessId);
Run Code Online (Sandbox Code Playgroud)
更新:
这是我现在尝试过的:
这是我构建 okhttpclient 的方法:
final OkHttpClient.Builder builder = new OkHttpClient.Builder();
int cacheSize = 10 * 1024 * 1024; // …Run Code Online (Sandbox Code Playgroud) 我在尝试验证请求规范时遇到问题。如何在每个 http 请求的标头中传递有效的身份验证令牌?我下面的方法正确吗?
tweets_request_spec.rb
require 'rails_helper'
RSpec.describe 'Tweets API', type: :request do
before do
@tweets = create_list(:tweet, 10)
@tweet = @tweets.first
end
describe 'GET /tweets' do
before { get '/tweets', { "Authorization": *some sort of token*} }
it "returns tweets" do
expect(json).to_not be_empty
expect(json).to eq(10)
end
it "is a successful http request" do
expect(response).to have_http_response(200)
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的身份验证控制器代码,以及帮助生成和解码在 http 标头中传递的身份验证令牌的模块。
身份验证控制器.rb
class AuthenticationController < ApplicationController
skip_before_action :authenticate_request
def authenticate
command = AuthenticateUser.call(params[:email], params[:password])
if command.success?
render json: { auth_token: …Run Code Online (Sandbox Code Playgroud) enable-cors.org nginx 配置建议使用以下值Access-Control-Allow-Headers和Access-Control-Expose-Headers。但除了 之外,没有太多解释为什么推荐这些Custom headers and headers various browsers *should* be OK with but aren't。如果我的应用程序不需要其中一些 API 请求,我宁愿不增加每个 API 请求的有效负载。
我知道我可以删除它们并等待某些东西损坏,但我希望了解一些关于为什么/如何选择它们的背景,以便我可以就它们是否对我的应用程序是必要的做出更明智的决定。即他们是否建议支持我的应用程序不需要支持的浏览器?
访问控制允许标头:DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range
访问控制公开标头:DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range
对于Allow-Headers大多数人来说,我可以理解为什么客户想要发送它们。 X-CustomHeader但作为一个奇怪的人脱颖而出。另外,我在 Chrome 上测试过,即使User-Agent明确允许,chrome 仍然会发送它。这意味着添加这些选项是为了实现我的应用程序可能不需要的浏览器兼容性。
对于 来说Expose-Headers,客户端需要读取哪些标头似乎是非常特定于应用程序的。为什么客户需要阅读 User-Agent、、DNT或X-Requested-With?它们包含供服务器而不是客户端使用的信息。此外,Cache-Control和Content-Range已经默认启用,因此它们在这里看起来是多余的。
编辑:
问题:2和问题:3通过以下@melpomene 注释解决,即通过使用读取的字节数来打印缓冲区。
但仍然遇到问题: 1。
我写了一个TCP服务器-客户端程序。后来出于好奇,我想了解一下HTTP服务器。
我之前的问题:简单的 TCP 服务器无法输出到网络浏览器
现在我只是看看使用GETand POST(form-data和x-www-form-urlencoded现在)将数据传输到服务器的内容和方式。
我正在按照如何从命令行 cURL POST发送POST请求。
当我发送x-www-form-urlencoded为:
curl -d "data=example1&data2=example2" localhost:8080
Run Code Online (Sandbox Code Playgroud)
服务器上的输出:
POST / HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.54.0
Accept: */*
Content-Length: 28
Content-Type: application/x-www-form-urlencoded
data=example1&data2=example2
Run Code Online (Sandbox Code Playgroud)
这正如预期的那样。
问题:1
现在问题来了。当我尝试发送时form-data,输出不是预期的。
当我发送form-data为:
curl -X POST -F "name=user" -F "password=test" localhost:8080
Run Code Online (Sandbox Code Playgroud)
服务器上的输出:
POST / HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.54.0
Accept: …Run Code Online (Sandbox Code Playgroud) 我知道如何在 Swagger 中添加一个标头值,代码如下:
[HttpGet]
[Route("api/{bookID}")]
public async Task<IActionResult> GetBooksByID([FromHeader(Name = "Correlation-ID")]string bookID)
{
//...
}
Run Code Online (Sandbox Code Playgroud)
但是如果我想添加其他标头值(例如“TenantID”、“UserID”等)怎么办?
语法会是什么样子?
我在前端使用 Angular,在后端使用 Jersey。当我执行 PUT 请求时出现异常。这是角度代码:
const header=new Headers({'Content-Type':'application/x-www-form-urlencoded'});
header.append("Access-Control-Allow-Methods", "POST");
header.append("Access-Control-Allow-Headers","Access-Control-Allow-Origin");
return this.http.post('http://localhost:8080',home,{headers: header})
.pipe(map((response: Response)=>{return response.json();}));
Run Code Online (Sandbox Code Playgroud)
这是我在泽西岛的过滤器:
@Provider
public class CORSResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
//headers.add("Access-Control-Allow-Origin", "http://podcastpedia.org"); podcastpedia.org
responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,PATCH,OPTIONS");
responseContext.getHeaders().add("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
}
}
Run Code Online (Sandbox Code Playgroud)
这是例外:
无法加载http://localhost:8080/预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 Access-Control-Allow-Methods
有人可以帮助我吗?
当我使用 Ebay API 向 CreateOrReplaceInventoryItem 发出 PUT 请求时,出现以下错误:
误用标头名称。确保请求标头与 HttpRequestMessage 一起使用,响应标头与 HttpResponseMessage 一起使用,内容标头与 HttpContent 对象一起使用。
下面的代码抛出错误。
using (var client = new HttpClient())
{
var baseUri = new Uri(string.Format("https://api.sandbox.ebay.com/sell/inventory/v1/inventory_item/{0}", SKU));
client.DefaultRequestHeaders.Add("Authorization", string.Format("Bearer {0}", token));
client.DefaultRequestHeaders.Add("Content-Language", "en-US");
var payload = JsonConvert.SerializeObject(product);
var content = new StringContent(payload, Encoding.UTF8, "application/json");
var response = client.PutAsync(baseUri, content).Result;
return response.Content.ReadAsStringAsync().Result;
}
Run Code Online (Sandbox Code Playgroud)
如果我删除该行:
client.DefaultRequestHeaders.Add("Content-Language", "en-US");
Run Code Online (Sandbox Code Playgroud)
然后我得到一个返回的错误对象:
errors":[{"errorId":25709,"domain":"API_INVENTORY","subdomain":"Selling","category":"REQUEST","message":"Invalid value for header Content-Language."}]}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?还有另一种我不知道的设置内容语言的方法吗?
@GET("/city/{cityId}/category/all")
Observable<MyDictionary> getDictionaries(@Path(value = "cityId", encoded = true) String cityId, @HeaderMap Map<String, String> headers);
Run Code Online (Sandbox Code Playgroud)
调用方法检索数据:
service.getDictionaries(cityId, headersMap)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(resp ->
{
/*...............*/
}, throwable ->
{
/*...............*/
});
Run Code Online (Sandbox Code Playgroud)
我可以轻松得到答复。但我还需要知道响应头。如何在我的代码中访问它们?有小费吗?我只获取正文,如何获取标题?