简体   繁体   中英

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. 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

The next step that I did is to create an HTTP connection and send the feed over with the POST request. This is what I have in my client code and example here is the feed name.

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. I would like some help on trying to resolve this issue. Maybe I am not parsing it correctly.

Update #2

I have tried to parse the HTTP headers to get to the payload like comments have said. 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. 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. Right now, you're sending the http headers to protobuf, which doesn't make sense. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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