客户端使用服务端API接口时,需构造HTTP请求头,一般情况下是初始化一个NSMutableURLRequest,然后设置请求方法、请求体,请求头,如下:
猿题库的网络请求( YTKNetwork )已把请求方法、请求体,请求头等封装好允许用户重载进行自定义。包括:
请求头通过重写方法 - (NSDictionary *)requestHeaderFieldValueDictionary; 返回一个字典,然后在方法
- (AFHTTPRequestSerializer *)requestSerializerForRequest:(YTKBaseRequest *)request; 中将网络请求序列化,供构造NSURLSessionTask使用。
下面具体说说请求头里几个字段的含义:
1、1** 信息,服务器收到请求,需要请求者继续执行 *** 作
2、2** 成功, *** 作被成功接收并处理
3、3** 重定向,需要进一步的 *** 作以完成请求
4、4** 客户端错误,请求包含语法错误或无法完成请求
5、5** 服务器错误,服务器在处理请求的过程中发生了错误
4种为HttpClient添加默认请求头的方式。下面详细的介绍;第一种方式
直接在创建的HttpClient对象的DefaultRequestHeaders集合中添加报头。
class Program
{undefined
static Task Main()=>SendAsync1()
private static async Task SendAsync1()
{undefined
var httpClient = new HttpClient()
AddDefaultHeaders(httpClient)
await httpClient.GetStringAsync("http://localhost:5000/")
}
private static void AddDefaultHeaders(HttpClient httpClient)
{undefined
httpClient.DefaultRequestHeaders.Add("x-www-foo", "123")
httpClient.DefaultRequestHeaders.Add("x-www-bar", "456")
httpClient.DefaultRequestHeaders.Add("x-www-baz", "789")
}
}
第二种方式
对于.NET Core应用来说,我们更推荐的做法是采用依赖注入的方式,利用IHttpClientFactory来创建HttpClient对象,那么我们在进行相关服务注册的时候就可以设置默认请求报头。
class Program
{undefined
static Task Main()=>SendAsync2()
private static async Task SendAsync2()
{undefined
var services = new ServiceCollection()
services.AddHttpClient("", AddDefaultHeaders)
var httpClient = services
.BuildServiceProvider()
.GetRequiredService()
.CreateClient()
await httpClient.GetStringAsync("http://localhost:5000/")
}
private static void AddDefaultHeaders(HttpClient httpClient)
{undefined
httpClient.DefaultRequestHeaders.Add("x-www-foo", "123")
httpClient.DefaultRequestHeaders.Add("x-www-bar", "456")
httpClient.DefaultRequestHeaders.Add("x-www-baz", "789")
}
}
第三种方式
由于HttpClient在发送请求的时候会利用DiagnosticSource对象发送相应的诊断事件,并且将作为请求的HttpRequestMessage对象作为请求事件内容负载。我们可以订阅该事件,在请求被发送之前将其拦截下来,并添加相应的请求头即可。
class Program
{undefined
static Task Main()=>SendAsync3()
private static async Task SendAsync3()
{undefined
Func requestAccessor = null
DiagnosticListener.AllListeners.Subscribe(listener =>
{undefined
if (listener.Name == "HttpHandlerDiagnosticListener")
{undefined
listener.Subscribe(kv =>
{undefined
if (kv.Key == "System.Net.Http.HttpRequestOut.Start")
{undefined
requestAccessor ??= BuildRequestAccessor(kv.Value.GetType())
var request = requestAccessor(kv.Value)
AddDefaultHeaders(request)
}
})
}
})
var httpClient = new HttpClient()
await httpClient.GetStringAsync("http://localhost:5000/")
static Func BuildRequestAccessor(Type payloadType)
{undefined
var property = payloadType.GetProperty("Request", BindingFlags.Instance | BindingFlags.Public)
var payload = Expression.Parameter(typeof(object))
var convertedPayload = Expression.Convert(payload, payloadType)
var getRequest = Expression.Call(convertedPayload, property.GetMethod)
var convert = Expression.Convert(getRequest, typeof(HttpRequestMessage))
return Expression.Lambda>(convert, payload).Compile()
}
}
private static void AddDefaultHeaders(HttpRequestMessage request)
{undefined
request.Headers.Add("x-www-foo", "123")
request.Headers.Add("x-www-bar", "456")
request.Headers.Add("x-www-baz", "789")
}
}
第四种方式
上面这种方式可以采用强类型编程方式,具体的代码如下。
class Program
{undefined
static Task Main()=>SendAsync4()
private static async Task SendAsync4()
{undefined
DiagnosticListener.AllListeners.Subscribe(listener =>
{undefined
if (listener.Name == "HttpHandlerDiagnosticListener")
{undefined
listener.SubscribeWithAdapter(new HttpClientListener())
}
})
var httpClient = new HttpClient()
await httpClient.GetStringAsync("http://localhost:5000/")
}
private sealed class HttpClientListener
{undefined
[DiagnosticName("System.Net.Http.HttpRequestOut.Start")]
public void OnSend(HttpRequestMessage request) =>AddDefaultHeaders(request)
//Must subscribute the System.Net.Http.HttpRequestOut event.
[DiagnosticName("System.Net.Http.HttpRequestOut")]
public void OnSend() { }
}
private static void AddDefaultHeaders(HttpRequestMessage request)
{undefined
request.Headers.Add("x-www-foo", "123")
request.Headers.Add("x-www-bar", "456")
request.Headers.Add("x-www-baz", "789")
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)