gpt4 book ai didi

java - 对tomcat的套接字http请求,但响应302

转载 作者:行者123 更新时间:2023-11-28 22:46:42 24 4
gpt4 key购买 nike

当我使用 Postman 使用 url 获取结果时:“localhost:8080/examples”,我可以获得正确的结果:

<!--
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 版本:8.5.32

但是当我使用套接字从 tomcat 服务器获取结果时,结果让我感到困惑。代码在这里:


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();
}
}

代码结果日志在这里:

[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.1Host: localhost:8080Cache-Control: no-cachePostman-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: chunkedDate: Sun, 01 Sep 2019 08:50:08 GMT0HTTP/1.1 302 Location: /examples/Transfer-Encoding: chunkedDate: Sun, 01 Sep 2019 08:50:08 GMT0

我不知道为什么,请帮帮我

最佳答案

响应码302表示重定向。 postman 可能会跟随它,给你结果。

在我看来,在这些情况下最简单的事情是使用像 Apache HttpClient 这样的简单库发出请求。

这是学习 HttpClient 库的有用教程:https://www.tutorialspoint.com/apache_httpclient/index.htm

如何自动跟随重定向:Handling HttpClient Redirects

关于java - 对tomcat的套接字http请求,但响应302,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57744639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com