简体   繁体   English

协议消息包含无效标签0

[英]Protocol message contained an invalid tag 0

My goal is to create a realtime service alert feed and send it over to a server that I made in Java with an HTTP post request. 我的目标是创建一个实时服务警报提要,并将其发送到我用Java发布的带有HTTP发布请求的服务器上。 The first step I did was to create a copy of the example alert feed posted here and it seems I was successfully able to do that as I was able to print it out the message. 我要做的第一步是创建此处发布的示例警报提要的副本,由于能够将其打印出消息,因此我似乎能够成功做到这一点。 https://developers.google.com/transit/gtfs-realtime/examples/alerts https://developers.google.com/transit/gtfs-realtime/examples/alerts

The next step that I did is to create an HTTP connection and send the feed over with the POST request. 我要做的下一步是创建HTTP连接,并通过POST请求发送供稿。 This is what I have in my client code and example here is the feed name. 这是我的客户代码中的内容,此处的示例是Feed名称。

String url = "https://localhost:8080";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-Type", "application/x-protobuf");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
example.build().writeDelimitedTo(wr);
wr.flush();
wr.close();

My server code is simply this so far. 到目前为止,我的服务器代码就是这样。

ServerSocket server = new ServerSocket(8080); 
System.out.println("Listening for connection on port 8080 ...."); 
while (true) { 
    try (Socket socket = server.accept()) {
      FeedMessage feed = FeedMessage.parseDelimitedFrom(socket.getInputStream());
      Date today = new Date(); 
      String httpResponse = "HTTP/1.1 200 OK\r\n\r\n" + today; 
      socket.getOutputStream().write(httpResponse.getBytes("UTF-8")); 
      }     
} 

The question here is that I get the Protocol message contained an invalid tag 0 on the server side. 这里的问题是我在服务器端收到的协议消息包含无效标记0。 I would like some help on trying to resolve this issue. 在解决此问题上,我需要一些帮助。 Maybe I am not parsing it correctly. 也许我没有正确解析它。

Update #2 更新#2

I have tried to parse the HTTP headers to get to the payload like comments have said. 我已经尝试解析HTTP标头来获得有效负载,如评论所说。 But my code hangs and the output to print the headers on the terminal looks serialized. 但是我的代码挂起了,在终端上打印标题的输出看起来已经序列化了。

    DataInputStream isr = new DataInputStream(socket.getInputStream());
    Reader reader = new InputStreamReader(isr); 
    BufferedReader reader2 = new BufferedReader(reader);  
    String line = reader2.readLine();
    System.out.println("get lines");
    while (!line.isEmpty()) { 
         System.out.println(line); 
         line = reader2.readLine(); 
     }

You are dealing with raw sockets at the server, but the payload is encoded in an http request body. 您正在服务器上处理原始套接字,但是有效负载已编码在http请求正文中。 You are going to need to parse the http request through to the payload, and when you have just the body : send that to protobuf. 您将需要解析http请求到有效负载,并且当您只有body时 :将其发送到protobuf。 Right now, you're sending the http headers to protobuf, which doesn't make sense. 现在,您正在将http标头发送到protobuf,这没有任何意义。 That could mean parsing through to \\r\\n\\r\\n , but it would help if you could make use of the content-length header, and even better if you can use a pre-built http library. 这可能意味着解析到\\r\\n\\r\\n ,但是如果您可以使用content-length标头,则将有所帮助,如果可以使用预构建的http库,则更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM