【问题标题】:Using curl with a Java program在 Java 程序中使用 curl
【发布时间】:2012-07-22 15:05:24
【问题描述】:

对于作业,我必须创建一个使用休息的程序。这是老师给我的代码,让我们开始这个作业,所以下面的代码应该是正确的。

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”请求方法进行了尝试,并得到了相同的结果。

【问题讨论】:

    标签: java curl httprequest


    【解决方案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 等,直到出现问题。如果您在逐位输入时没有任何中断,则怀疑是复制/粘贴。尝试使用不同的数据。理智检查选项。
    【解决方案2】:
    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 2014-07-26
      • 2011-02-04
      • 2016-06-11
      相关资源
      最近更新 更多