2016-07-31 103 views
3

我有一個java echo httpserver up。在Java HttpServer響應中設置內容類型

它適用於測試網站,但我正在使用的客戶端代碼無法獲取數據。

服務器用https://www.hurl.it/

客戶端完美支援https://requestb.in/http://httpbin.org/post

當兩者結合我得到的200狀態碼的響應作品完美,但沒有元數據/正文內容中顯示出來客戶即使正在發送。

我唯一的猜測是因爲內容類型不包含在內,客戶端可能會挑剔。

如何在我的回覆中指定內容類型?

(一張紙條,客戶端發送一個字符串到服務器用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

嘗試寫

+0

對我來說,'t.getResponseHeaders之前添加

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

()。添加(.....)''之前sendResponseHeaders ' – asr