我試圖製作一個可以接受並使用POST返回數據的Java服務器。到目前爲止,我一直試圖使用nameValuePairs(儘管另一種方法是好的),並讓服務器接受連接,並接受HTTP頭,但從這裏我無法理解如何獲得任何附加數據。以下代碼輸出標題並在客戶端期望它時發送100響應 - 但是如何從消息中獲取數據?使用Java檢索POST數據
while(true){
inputLine = clientInput.readLine();
System.out.println("client-msg: " + inputLine);
if("Expect:".equals(inputLine.split(" ")[0])){
sendResponse(100, "Continue");
break;
}
這裏是什麼(機器人JAVA)客戶端發送的,我希望得到的名稱對出在服務器端
ArrayList<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("field", "data here"));
UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(
postParams);
request.setEntity(uefe);
HttpResponse response = client.execute(request);
這可能是相當簡單的,但我做不到似乎找到任何非PHP的POST教程。
自從Java 6 Java帶有一個HTTP服務器(即:不需要嵌入Jetty)。如果你是在一個非常輕量級的HTTP服務器之後,你可能想要Google for NanoHTTPD。 – SyntaxT3rr0r 2011-01-23 15:48:25