3

課題のために、残りを使用するプログラムを作成する必要があります。これは、この課題を開始するために先生から与えられたコードなので、以下のコードは正しいはずです。

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 クラスには「POST」をチェックする if ステートメントがあるため、それを使用してテストします。したがって、別の端末から 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」オプションを使用していないため、プログラムは要求メソッドを「POST」ではなく「GET」として読み取っています。「DELETE」および「PUT」リクエストメソッドでこれを試してみましたが、同じ結果が得られました。

4

2 に答える 2

1

それはダッシュではありません:

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

発生するエラーはからのものであるため、コードが完全に無関係であることは明らかですcurl

コードは正しいはずですが、それは正しいという意味でありません。先生、本、ウェブサイトなどから入手したからといって、それを信用しないでください。カットアンドペーストの問題など、あらゆる種類の問題が発生する可能性があります。これは、curlコマンドでも発生する可能性があります。

于 2012-07-22T15:15:43.287 に答える
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

これらはカールエラーであり、リモートホストが原因ではありません。カールリクエストを調査した後、間違った「-」文字を使用します。

リアルオプションが-dの場合、-dを使用しています。

サイズの違いをご覧ください。

  • –d<-間違っている
  • -d<-正解
于 2012-07-22T15:17:18.880 に答える