【问题标题】:Writing Client Program for Protocol Buffer service为 Protocol Buffer 服务编写客户端程序
【发布时间】:2014-01-06 06:56:14
【问题描述】:

我在远程机器上运行了一个协议缓冲区服务,比如https://website/protobufService 在用户指南中,我只看到消息结构。像 :

message Request {
 required String name = 1;
 required int id = 2,

 ........
}

message Response {
 optional int result =1;
  enum Code {
    val1 = 1;
    val2 = 2;
  } 
 optional Code = 2;
}

我不知道如何用 Java 编写客户端代码来使用此服务?

是否有关于为 Protobuf 服务编写客户端代码的指南?

提前非常感谢。 - 乙脑

【问题讨论】:

    标签: protocol-buffers webservice-client


    【解决方案1】:

    Protobufs 仅指定如何将消息转换为字节并返回,而不是如何通过网络发送消息。特别是,没有通过 HTTP 发送 protobuf 的标准方法。您尝试使用的特定服务的文档需要准确指定它希望如何发送消息。

    不过,通常情况下,您会向 URL 发送一个 HTTP POST,并将编码的 Request protobuf 作为其主体,它会以编码的 Response protobuf 进行响应。您应该能够使用任何旧的 HTTP 库来执行此操作;只需将编码的Request 作为字节给出,并将响应字节解码为Response

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。我为我的 Web 服务编写了以下客户端。幸运的是它确实返回了一个输出。不幸的是我认为它不是 proto-buf 格式。我想以 protobuff 格式查看它。我不知道 protobuff 格式是什么样子的。你可能想看看下面的链接:- https://spring.io/blog/2015/03/22/using-google-protocol-buffers-with-spring-mvc-based-rest-services

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.net.HttpURLConnection;
      import java.net.MalformedURLException;
      import java.net.URL;
      
      public class HttpRestClient {
      
      // http://localhost:8080/customers/
      public static void main(String[] args) {
      
          try {
      
              URL url = new URL("http://localhost:8080/customers/1");
      
              HttpURLConnection conn = (HttpURLConnection) url.openConnection();
              conn.setRequestMethod("GET");
              conn.setRequestProperty("Content-Type", "application/x-protobuf");
              conn.setRequestProperty("Accept", "application/x-protobuf");
      
              String contentType = conn.getContentType();
              System.out.println(contentType);
              System.out.println("---------------------------------------------------------------------");
      
              if (conn.getResponseCode() != 200) {
                  throw new RuntimeException("Failed : HTTP error code : " + 
      conn.getResponseCode());
              }
      
              BufferedReader br = new BufferedReader(new 
      InputStreamReader((conn.getInputStream())));
      
              String output;
              System.out.println("Output from Server .... \n");
              while ((output = br.readLine()) != null) {
                  Object response = output;
                  System.out.println(response);
              }
      
              conn.disconnect();
      
          } catch (MalformedURLException e) {
      
              e.printStackTrace();
      
          } catch (IOException e) {
      
              e.printStackTrace();
      
          }
      
      }
      
      }
      

      如果有人有更好的建议,请告诉我。这是我的输出的样子:-

      application/x-protobuf;charset=UTF-8

      来自服务器的输出 ....

      克里斯 理查森* cricharson@email.com

      如您所见,它不是 protobuff 格式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-31
        • 1970-01-01
        • 1970-01-01
        • 2013-05-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多