使用curl与Java程序配合使用

3

我需要创建一个使用REST的程序,老师给我们提供了以下代码作为起点,应该是正确的。

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从单独的终端与该程序通信时,我输入:

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 = [*/*]

我觉得当我在终端使用curl时,我总是犯错。通过查看错误,它无法接受我输入的"-d"选项,这导致程序将请求方式读取为"GET"而不是"POST"。我已经尝试了"DELETE"和"PUT"请求方法,并得到了相同的结果。

2个回答

1

那不是破折号:

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

应该清楚地知道,代码是完全无关紧要的,因为你得到的错误来自于curl

虽然代码应该是正确的,但这并不意味着它就是正确的。不要仅仅因为你从老师、书籍、网站等处得到了代码就信任它。各种问题都可能出现,比如剪切和粘贴问题,这很可能也是你的curl命令出错的原因。


哇,当然了,我的错误肯定是那么简单的事情。谢谢! - user1513200
虽然我一开始就怀疑了,但你也可以使用排除法:curl localhostcurl localhost:8080等等,直到出现错误为止。如果在逐位输入时没有出现任何错误,请怀疑复制/粘贴。尝试使用不同的数据。对选项进行合理性检查。 - Dave Newton

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错误,不是由远程主机引起的。在调查您的curl请求后,发现您使用了错误的“-”字符。

您正在使用–d,而真正的选项是-d。

请注意大小写的区别:

  • –d <- 错误
  • -d <- 正确

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接