|
描述
Protobuf是Google protocol buffer的简称,是一种语言中立、平台无关、易于扩展的结构化数据序列化技术,可用于数据传输、存储等领域。
与Protoful类似的序列化技术还有XML、JSON、Thrift等,但Protoful更快、更小、更简单,且具备良好的兼容性。
配置请求头
方法一
只设置客户端请求时附带的header
类 io.grpc.stub.MetadataUtils,其中有个方法
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1789")
public static <T extends AbstractStub<T>> T attachHeaders(
T stub,
final Metadata extraHeaders) {
return stub.withInterceptors(newAttachHeadersInterceptor(extraHeaders));
}自己封装后
private static <T extends AbstractStub<T>> T attachHeaders(T stub, final Map<String, String> headerMap) {
Metadata extraHeaders = new Metadata();
if (headerMap != null) {
for (String key : headerMap.keySet()) {
Metadata.Key<String> customHeadKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
extraHeaders.put(customHeadKey, headerMap.get(key));
}
}
return MetadataUtils.attachHeaders(stub, extraHeaders);
}
方法二
支持设置客户端请求的header以及获取服务端返回结果中的header
1. 设置拦截器
class HeaderClientInterceptor implements ClientInterceptor {
private static final String TAG = &#34;HeaderClientInterceptor&#34;;
private Map<String, String> mHeaderMap;
public HeaderClientInterceptor(Map<String, String> headerMap) {
mHeaderMap = headerMap;
}
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions, Channel next) {
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
/* put custom header */
if (mHeaderMap != null) {
for (String key : mHeaderMap.keySet()) {
Metadata.Key<String> customHeadKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
headers.put(customHeadKey, mHeaderMap.get(key));
}
}
Logger.i(TAG, &#34;header send to server:&#34; + headers);
super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
@Override
public void onHeaders(Metadata headers) {
/**
* if you don&#39;t need receive header from server,
* you can use {@link io.grpc.stub.MetadataUtils attachHeaders}
* directly to send header
*/
Logger.i(TAG, &#34;header received from server:&#34; + headers);
super.onHeaders(headers);
}
}, headers);
}
};
}
}
2.使用
Map<String, String> headerMap = new HashMap<>();
//...
ClientInterceptor interceptor = new HeaderClientInterceptor(headerMap);
//...
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build();
Channel channel = ClientInterceptors.intercept(managedChannel, interceptor);
// then create stub here by this channel所以
要实现添加header 那么必须实现 ClientInterceptor 接口类中的 interceptCall 的方法
而且还要有一个 添加Header的具体方法
不管 grpc 怎么混淆都离不开这种配置模式
例如这种
注意他们的类型,很明显这种情况可以快速推论
z1.c.v.p.a.d.b.f.a.a() 大概率就是混淆后的 interceptCall
z1.c.v.p.a.d.b.f.a.c() 是添加请求头的函数
了解更多 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|