2016-07-31 4 views
3

나는 java echo server를 사용하고있다.자바에서 콘텐츠 유형 설정 HttpServer 응답

테스트 사이트에서 작동하지만, 작동중인 클라이언트 코드가 데이터를 가져 오지 못합니다.

는 서버가 https://www.hurl.it/

클라이언트는 https://requestb.in/와 함께 완벽하게 작동하고

두 내가 200 상태 코드와 응답을 결합 http://httpbin.org/post 완벽하게 작동하지만 메타 데이터/본문 내용은에서 보여줍니다 클라이언트가 전송 되더라도.

콘텐츠 유형이 포함되어 있지 않기 때문에 추측 할 수 있습니다. 클라이언트는 그것에 대해 까다 롭습니다.

내 응답에 콘텐츠 유형을 어떻게 지정할 수 있습니까?

(참고는, 클라이언트가 어떤 헤더 정보와 함께 매개 변수로 POST로 서버에 하나의 문자열을 보냅니다.이 코드는 반환 본문 내용/매개 변수에 현재 설정입니다.)

어떤 아이디어 고맙다!

import static java.net.HttpURLConnection.HTTP_OK; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.InetSocketAddress; 
import java.net.URLDecoder; 
import java.util.List; 

import com.sun.net.httpserver.Headers; 
import com.sun.net.httpserver.HttpExchange; 
import com.sun.net.httpserver.HttpHandler; 
import com.sun.net.httpserver.HttpServer; 

/** 
* Echo the body of an HTTP request back as the HTTP response. This is merely 
* a simple exercise of the Secret Sun Web Server. As configured, the URL to 
* access it is http://localhost:8000/echo. 
* 
* @author Andrew Cowie 
*/ 
public final class Test 
{ 
public static void main(String[] args) throws IOException { 
    final InetSocketAddress addr; 
    final HttpServer server; 

    addr = new InetSocketAddress(8000); 

    server = HttpServer.create(addr, 10); 
    server.createContext("/echo", new EchoHandler()); 
    server.start(); 
} 
} 

class EchoHandler implements HttpHandler 
{ 
public void handle(HttpExchange t) throws IOException { 
    final InputStream is; 
    final OutputStream os; 
    StringBuilder buf; 
    int b; 
    final String request, response; 

    buf = new StringBuilder(); 

    /* 
    * Get the request body and decode it. Regardless of what you are 
    * actually doing, it is apparently considered correct form to consume 
    * all the bytes from the InputStream. If you don't, closing the 
    * OutputStream will cause that to occur 
    */ 

    is = t.getRequestBody(); 

    while ((b = is.read()) != -1) { 
     buf.append((char) b); 
    } 

    is.close(); 

    if (buf.length() > 0) { 
     request = URLDecoder.decode(buf.toString(), "UTF-8"); 
    } else { 
     request = null; 
    } 

    /* 
    * Construct our response: 
    */ 

    buf = new StringBuilder(); 
    //buf.append("<html><head><title>HTTP echo server</title></head><body>"); 
    //buf.append("<p><pre>"); 
    //buf.append(t.getRequestMethod() + " " + t.getRequestURI() + " " + t.getProtocol() + "\n"); 

    /* 
    * Process the request headers. This is a bit involved due to the 
    * complexity arising from the fact that headers can be repeated. 
    */ 

    Headers headers = t.getRequestHeaders(); 

    for (String name : headers.keySet()) { 
     List<String> values = headers.get(name); 

     for (String value : values) { 
      //buf.append(name + ": " + value + "\n"); 
     } 
    } 

    /* 
    * If there was an actual body to the request, add it: 
    */ 

    if (request != null) { 
     //buf.append("\n"); 
     buf.append(request); 
    } 

    //buf.append("</pre></p>"); 
    //buf.append("</body></html>\n"); 

    response = buf.toString(); 
    System.out.println(response); 

    /* 
    * And now send the response. We could have instead done this 
    * dynamically, using 0 as the response size (forcing chunked 
    * encoding) and writing the bytes of the response directly to the 
    * OutputStream, but building the String first allows us to know the 
    * exact length so we can send a response with a known size. Better :) 
    */ 

    t.sendResponseHeaders(HTTP_OK, response.length()); 

    os = t.getResponseBody(); 

    os.write(response.getBytes()); 

    /* 
    * And we're done! 
    */ 

    os.close(); 
    t.close(); 
} 

}

답변

3

시도 ', 내 경우에는 t.getResponseHeaders을

+0

을 쓰기 전에

t.getResponseHeaders().put("Content-Type", "text/html"); 

를 추가하는(). 추가 (.....)'sendResponseHeaders'전 ' – asr

관련 문제