Android如何在grpc客户端中添加自定义标头?

时间:2017-07-16 06:10:06

标签: android client metadata grpc grpc-java

我必须在android grpc客户端中添加自定义标头。我无法成功发送。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.fa {
  padding: 20px;
  font-size: 30px;
  width: 30px;
  text-align: center;
  text-decoration: none;
  margin: 5px 2px;
  border-radius: 50%;
}

.fa:hover {
    opacity: 0.7;
}

.fa-facebook {
  background: #3B5998;
  color: white;
}

.fa-twitter {
  background: #55ACEE;
  color: white;
}

</style>
</head>
<body>

<h2>Style Social Media Buttons</h2>

<!-- Add font awesome icons -->
<a href="#" class="fa fa-facebook"></a>
<a href="#" class="fa fa-twitter"></a>
</body>
</html> 

编辑:成功添加自定义标题

现在在我的grpc电话中,我这样打电话,

public class HeaderClientInterceptor implements ClientInterceptor {
       @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 */
    Timber.d("header sending to server:");


    Metadata fixedHeaders=new Metadata();
    Metadata.Key<String> key =
        Metadata.Key.of("Grps-Matches-Key", Metadata.ASCII_STRING_MARSHALLER);
    fixedHeaders.put(key, "primary.secondary");

    headers.merge(fixedHeaders);

    super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
      @Override
      public void onHeaders(Metadata headers) {
        /**
         * if you don't need receive header from server,
         * you can use {@link io.grpc.stub.MetadataUtils attachHeaders}
         * directly to send header
         */

        Timber.e("header received from server:" + headers.toString());
        super.onHeaders(headers);
      }
    }, headers);
  }
};
}
}

///我构建上述请求并在伪调用中调用它,如下所示。

ClientInterceptor interceptor = new HeaderClientInterceptor();
Channel channel = ManagedChannelBuilder
           .forAddress(BuildConfig.HOST, BuildConfig.PORT)
           .build();
Channel channelWithHeader = ClientInterceptors
            .intercept(channel, interceptor);

ServiceGrpc.ServiceBlockingStub service = ServiceGrpc
    .newBlockingStub(channelWithHeader);
  

&#34; Grps-Matches-Key:primary.secondary&#34;

在Rest API调用中,我会将其添加为标题,如     builder.header(&#34; Grps-Matches-Key&#34;,&#34; primary.secondary&#34;);

希望这有帮助。

2 个答案:

答案 0 :(得分:10)

问题中的编辑版本也有效。在GRPC中,有许多方法可以添加标题(称为元数据)。我们可以使用拦截器在上面的问题中添加元数据,或者我们可以为客户端存根添加元数据,或者您可以在客户端存根通道中发出请求之前添加元数据。

// create a custom header
Metadata header=new Metadata();
Metadata.Key<String> key =
    Metadata.Key.of("Grps-Matches-Key", Metadata.ASCII_STRING_MARSHALLER);
header.put(key, "match.items");

// create client stub
ServiceGrpc.ServiceBlockingStub stub = ServiceGrpc
    .newBlockingStub(channel);

在使用MetadataUtils

进行任何请求之前添加标头
stub = MetadataUtils.attachHeaders(stub, header);

答案 1 :(得分:0)

对于 Kotlin 爱好者

    private fun getHeaderMetaData(): io.grpc.Metadata {
        val header: io.grpc.Metadata = io.grpc.Metadata()
        val channel: io.grpc.Metadata.Key<String> = io.grpc.Metadata.Key.of("channel", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
        val api_key: io.grpc.Metadata.Key<String> = io.grpc.Metadata.Key.of("api-key", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
        val lang: io.grpc.Metadata.Key<String> = io.grpc.Metadata.Key.of("lang", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
        header.put(channel, "AND")
        header.put(api_key, "dummykey")
        header.put(lang, "en")
        return header
    }

并调用此标头

   var stub = FlightSearchGrpc.newBlockingStub(channel)
   stub = io.grpc.stub.MetadataUtils.attachHeaders(stub, getHeaderMetaData())