2013-09-26 58 views
0

我想用POST調用我的HttpServer,並在服務器端發送消息,我可以看到它被調用兩次,我不知道爲什麼。 下面是客戶端代碼服務調用兩次

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

}

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

}

Finallty我看到System.out.println("body data: " + buffer.toString());輸出兩次

+0

您如何確定服務被調用兩次? – Rachel

+0

就像我說過的,在服務中我有一個打印出身體數據的日誌。我會有這樣的東西 「一些隨機的身體數據」 「一些隨機的身體數據」 –

+0

你可能會張貼更多的代碼?整個方法和可能調用這個方法的代碼?目前的代碼似乎沒有太明顯的錯誤。 – hooknc

回答

0

好了,我終於想通了,是怎麼回事... 我有一個方法 public synchronized boolean addValue(int id, int value){ Integer previous = values.put(id, value); return previous.intValue() != value; }

問題是,在第一時間,看跌期權將返回NULL值,只要我把這個護理方法錯誤不會再發生。