0

when I use Postman to get result using url : "localhost:8080/examples", and I can get the correct result :

<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!DOCTYPE HTML>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Apache Tomcat Examples</title>
    </head>
    <body>
        <p>
            <h3>Apache Tomcat Examples
            </H3>
            <p></p>
            <ul>
                <li>
                    <a href="servlets">Servlets examples</a>
                </li>
                <li>
                    <a href="jsp">JSP Examples</a>
                </li>
                <li>
                    <a href="websocket/index.xhtml">WebSocket Examples</a>
                </li>
            </ul>
        </body>
    </html>

tomcat version : 8.5.32

but when I use socket to get result from tomcat server, then the result confuse me. Code is here :


public void httpTest(){

        Socket socket = new Socket("localhost", 8080);
        data = "GET /examples HTTP/1.1\n" +
                "Host: localhost:8080\n" +
                "Cache-Control: no-cache\n" +
                "Postman-Token: 60236fcd-995c-49d9-896c-f88a1bdfbfd3\n\n".getBytes();

        log.debug("receive http request :\r\n{}", new String(data));


        try {

            //向服务器端发送数据
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            out.write(data);
            out.flush();

            //读取服务器端数据
            DataInputStream input = new DataInputStream(socket.getInputStream());
            byte[] result = readStream(input);

            log.debug("receive response :\r\n{}", new String(result));
            input.close();
            out.close();

            return result;
        } catch (Exception e) {
            log.error("请求本地服务数据异常, ", e);
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e2) {
                    socket = null;
                    log.error("客户端关闭socket异常, " ,e);
                }
            }
            socket = null;
            return _404();
        }
}

the code result log is here :

[DEBUG] 2019-09-01 16:50:08,304 method:com.jasey.reverse.client.core.local.LocalHostSender.send(LocalHostSender.java:18)
receive http request :
GET /examples HTTP/1.1
Host: localhost:8080
Cache-Control: no-cache
Postman-Token: 60236fcd-995c-49d9-896c-f88a1bdfbfd3


[DEBUG] 2019-09-01 16:50:28,364 method:com.jasey.reverse.client.core.local.LocalHostSender.send(LocalHostSender.java:37)
receive response :
HTTP/1.1 302 
Location: /examples/
Transfer-Encoding: chunked
Date: Sun, 01 Sep 2019 08:50:08 GMT

0


HTTP/1.1 302 
Location: /examples/
Transfer-Encoding: chunked
Date: Sun, 01 Sep 2019 08:50:08 GMT

0

I don't known why, please help me

Jasey Hu
  • 47
  • 1
  • 7
  • 1
    302 indicates a redirect response. Just follow the redirect. – Henry Sep 01 '19 at 09:07
  • 3
    Most probably because Postman automatically follows the redirect and makes a second request to /examples/ without you noticing it. If you want to do the same thing as Postman, follow the redirect too, i.e. read the Location header in the response, and send a second GET request to that location. Or use an actual HTTP client library that will do that for you. – JB Nizet Sep 01 '19 at 09:07
  • thank for your help! redirect /examples/ ! – Jasey Hu Sep 01 '19 at 09:26

1 Answers1

1

Response code 302 means Redirect. Postman probably follows it, giving you the result.

The simplest thing in these cases, in my opinion, is to make the request using a simple library like Apache HttpClient.

Here is a useful tutorial for learn HttpClient library: https://www.tutorialspoint.com/apache_httpclient/index.htm

Here how to automatically follow the redirects: Handling HttpClient Redirects

lorenzo
  • 627
  • 8
  • 12
  • 1
    Everybody loves the Apache httpd-client library for some reason, but I find it to be much more complicated than just using the built-in `java.net.HttpURLConnection`. But either way, you are better off using a component that understands HTTP instead of using a bare socket for communications. – Christopher Schultz Sep 04 '19 at 13:28