Java 服务呼叫了两次
我试图用POST调用我的HttpServer,并在正文中发送消息,在服务器端,我可以看到它被调用了两次,但我不知道为什么。 这是客户端代码的一部分Java 服务呼叫了两次,java,http,Java,Http,我试图用POST调用我的HttpServer,并在正文中发送消息,在服务器端,我可以看到它被调用了两次,但我不知道为什么。 这是客户端代码的一部分 String URL = "http://localhost:8081/" + path +"/service?session=" + sessionId; connection = openConnection(URL, "POST"); OutputStream output = connection.getOutputStream(); o
String URL = "http://localhost:8081/" + path +"/service?session=" + sessionId;
connection = openConnection(URL, "POST");
OutputStream output = connection.getOutputStream();
output.write("Some Random body data".getBytes());
output.close();
stream = connection.getInputStream();
stream.close();
connection.disconnect();
在服务器端,我可以看到服务被调用了两次。我想它与我的OutputStream和InputStream有关,但是如果我不调用输入流,它就不会在任何时候调用服务
编辑!!!
这里还有一些代码
公共类服务器{
private static final int BASE_PORT = 8081;
public static void main(String[] args) {
try{
InetSocketAddress address = new InetSocketAddress(BASE_PORT);
HttpServer server = HttpServer.create(address, 0);
server.createContext("/", new PathDelegator());
server.setExecutor(Executors.newCachedThreadPool());
server.start();
System.out.println("Server is listening on : " + BASE_PORT);
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
最后我看到
System.out.println(“主体数据:“+buffer.toString()”)代码>被输出了两次好吧,我终于弄明白发生了什么。。。
我有办法
public synchronized boolean addValue(int-id,int-value){
整数previous=values.put(id,value);
返回上一个.intValue()!=值;
}
问题是,第一次,put将返回一个空值,并且在我处理此方法后,错误不再发生。您如何确保服务被调用两次?正如我所说,在服务中,我有一个打印主体数据的日志。我会有这样的“一些随机体数据”“一些随机体数据”你可以发布更多的代码吗?整个方法以及调用此方法的可能代码?当前代码似乎没有太明显的错误。在原始帖子中添加了更多的代码比代码更好,您可以添加使用wireshark或tcpdump捕获的网络流量吗?
public class PathDelegator implements HttpHandler{
public void handle(HttpExchange exchange) throws IOException {
String URI = exchange.getRequestURI().toString();
if(URI.indexOf("/session") != -1){
//Call ServiceHandler
System.out.println("Call ServiceHandler");
serviceHandler(exchange, "some session key");
}
}
private void serviceHandler(HttpExchange exchange, String sessionId) throws IOException{
String requestMethod = exchange.getRequestMethod();
OutputStream responseBody = exchange.getResponseBody();
if(requestMethod.equalsIgnoreCase("POST")){
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
InputStream stream = exchange.getRequestBody();
int b = 0;
StringBuffer buffer = new StringBuffer();
while((b = stream.read()) != -1){
buffer.append((char)b);
}
System.out.println("body data: " + buffer.toString());
exchange.sendResponseHeaders(200, 0);
}else {
exchange.sendResponseHeaders(400, 0);
}
responseBody.close();
}
public class ClientTest {
@Test
public void shouldBeAbleToPostToService(){
try {
String SCORE_URL = "http://localhost:8081/service?session=" + sessionId;
connection = openConnection(URL, "POST");
OutputStream output = connection.getOutputStream();
output.write("Some body data".getBytes());
output.close();
stream = connection.getInputStream();
stream.close();
connection.disconnect();
fail("Not implemented yet!");
} catch (IOException e) {
e.printStackTrace();
}
}
private HttpURLConnection openConnection(String url, String method) throws IOException{
URL connectionURL = new URL(url);
HttpURLConnection connection = (HttpURLConnection)connectionURL.openConnection();
connection.setRequestMethod(method);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
return connection;
}