在Java程序中使用卷曲

在Java程序中使用卷曲

问题描述:

对于作业,我必须创建一个使用休息的程序。这是老师给我们的代码,让我们开始进行这项任务,所以下面的代码应该是正确的。在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”请求方法,并得到了相同的结果。

这不是一个破折号:

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

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 < - 正确