2012-07-22 30 views
2

對於作業,我必須創建一個使用休息的程序。這是老師給我們的代碼,讓我們開始進行這項任務,所以下面的代碼應該是正確的。在Java程序中使用捲曲

import java.io.*; 
import java.net.InetSocketAddress; 
import java.util.*; 
import java.util.concurrent.Executors; 

import com.sun.net.httpserver.*; 

public class HttpServerDemo { 
    public static void main(String[] args) throws IOException { 
     InetSocketAddress addr = new InetSocketAddress(8080); 
     HttpServer server = HttpServer.create(addr, 0); 
     server.createContext("/", new RootHandler()); 
     server.createContext("/foo/", new FooHandler()); 
     server.setExecutor(Executors.newCachedThreadPool()); 
     server.start(); 
     System.out.println("Server is listening on port 8080"); 
    } 

    public static void printHeaders(HttpExchange exchange, PrintStream response) { 
     Headers requestHeaders = exchange.getRequestHeaders(); 
     Set<String> keySet = requestHeaders.keySet(); 
     Iterator<String> iter = keySet.iterator(); 
     while(iter.hasNext()) { 
      String key = iter.next(); 
      response.println(key + " = " + requestHeaders.get(key)); 
     } 
    } 
    public static void printBody(HttpExchange exchange, PrintStream response) throws IOException { 
     BufferedReader body = new BufferedReader(new InputStreamReader(exchange.getRequestBody())); 
     String bodyLine; 
     while((bodyLine = body.readLine()) != null) { 
      response.println(bodyLine); 
     } 
    } 
} 

class RootHandler implements HttpHandler { 
    public void handle(HttpExchange exchange) throws IOException { 
     String requestMethod = exchange.getRequestMethod(); 

     Headers responseHeaders = exchange.getResponseHeaders(); 
     responseHeaders.set("Content-Type", "text/plain"); 
     exchange.sendResponseHeaders(200, 0); 

     PrintStream response = new PrintStream(exchange.getResponseBody()); 
     response.println("context: ROOT; method: " + requestMethod); 
     response.println("--- headers ---"); 
     HttpServerDemo.printHeaders(exchange, response); 
     if(requestMethod.equalsIgnoreCase("POST")) { 
      response.println("=== body ==="); 
      HttpServerDemo.printBody(exchange, response); 
     } 
     response.close(); 
    } 
} 

class FooHandler implements HttpHandler { 
    public void handle(HttpExchange exchange) throws IOException { 
     String requestMethod = exchange.getRequestMethod(); 

     Headers responseHeaders = exchange.getResponseHeaders(); 
     responseHeaders.set("Content-Type", "text/plain"); 
     exchange.sendResponseHeaders(200, 0); 

     PrintStream response = new PrintStream(exchange.getResponseBody()); 
     response.println("context: FOO; method: " + requestMethod); 
     HttpServerDemo.printHeaders(exchange, response); 
     response.close(); 
    } 
} 

由於RootHandler類有一個if語句來檢查「POST」,我將用它來測試它。所以,當我使用捲曲從一個單獨的終端與這個節目,我進入通信:

curl –d "message=helloworld" http://localhost:8080/ 

,我得到這個回報:

curl: (6) Could not resolve host: –d; nodename nor servname provided, or not known 
curl: (6) Could not resolve host: message=helloworld; nodename nor servname provided, or not known 
context: ROOT; method: GET 
--- headers --- 
Host = [localhost:8080] 
User-agent = [curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5] 
Accept = [*/*] 

我覺得我做我的錯誤,當我使用從我的終端捲起。通過查看錯誤,它不採用「-d」選項,我給它,它導致程序讀取請求方法爲「GET」而不是「POST」。我試過這個「DELETE」和「PUT」請求方法,並得到了相同的結果。

回答

1

這不是一個破折號:

curl –d "message=helloworld" http://localhost:8080/ # Not a dash 
curl -d "message=helloworld" http://localhost:8080/ # Is a dash 

應該清楚的是,代碼是完全不相干的,因爲你得到的錯誤是curl

儘管代碼應該是正確的,但這並不意味着它。不要僅僅因爲你是從老師,書本,網站等獲得它而信任它。各種各樣的事情都可能出錯,如剪切粘貼問題,這也可能是您的curl命令發生的情況。

+0

哇當然,我的錯誤是一些簡單。謝謝! – user1513200 2012-07-22 15:27:15

+0

@ user1513200雖然我從一開始就懷疑它,但您也可以使用消除的過程:'curl localhost','curl localhost:8080'等,直到出現故障。如果沒有任何內容打破你的輸入,可能會有點複製/粘貼。嘗試使用不同的數據。理智 - 檢查選項。 – 2012-07-22 15:37:13

0
curl: (6) Could not resolve host: –d; nodename nor servname provided, or not known 
curl: (6) Could not resolve host: message=helloworld; nodename nor servname provided, or not known 

這些都是捲曲錯誤,不是由遠程主機造成的。在調查你的curl請求之後,你使用了錯誤的「 - 」字符。

當真正的選項是-d時,您正在使用-d。

看到大小的區別了:

  • -d < - 錯
  • -d < - 正確