2013-01-01 57 views
-2

我正在編寫一個應用程序,它將通過HTTP將XML發送到服務器,並接收XML作爲響應。我能夠將XML發送到服務器,但無法接收響應。難以從HTTP客戶端接收響應

這是我的客戶端代碼:

public void sendXMLToServer(){ 
    System.out.println("sendXMLToServer"); 
    String strURL = "http://localhost:9080/MockServerMachine/sendXMLPost"; 
    // Get file to be posted 
    String strXMLFilename = "output.xml"; 
    File input = new File(strXMLFilename); 
    // Prepare HTTP post 
    System.out.println("junaud url "+ strURL); 
    PostMethod post = new PostMethod(strURL); 


// Request content will be retrieved directly 
    // from the input stream 
    // Per default, the request content needs to be buffered 
    // in order to determine its length. 
    // Request body buffering can be avoided when 
    // content length is explicitly specified 
    try { 
     post.setRequestHeader("Content-type","application/xml"); 
     post.setRequestHeader("Accept","application/xml"); 

     post.setRequestEntity(new InputStreamRequestEntity(
       new FileInputStream(input), input.length())); 
     HttpClient httpclient = new HttpClient(); 
     int result = httpclient.executeMethod(post); 
     String xmlResponse = post.getResponseBodyAsString(); 
     // Display status code 
     System.out.println("Response status code jun: " + result); 

     // Display response 
     System.out.println("Response body: "); 
     System.out.println(post.getResponseBodyAsString()); 
     post.releaseConnection(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (HttpException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

這是服務器端:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    //InputStream in = request.getInputStream(); 
     //URL xmlUrl = new URL(request.getRequestURL().toString()); 
    //InputStream in = xmlUrl.openStream(); 

    response.setContentLength(100); 
//  PostMethod po = new PostMethod(request.getRequestURL().toString()); 
//  System.out.println("kikmk = "+po.getRequestEntity()); 

    try { 
     // read this file into InputStream 
     //InputStream inputStream = new FileInputStream("c:\\file.xml"); 
     InputStream inputStream = request.getInputStream(); 
     // write the inputStream to a FileOutputStream 
     OutputStream out = new FileOutputStream(new File("c:\\junaidAhmedJameel.xml")); 

     int read = 0; 
     byte[] bytes = new byte[1024]; 

     while ((read = inputStream.read(bytes)) != -1) { 
      System.out.println(new String (bytes)); 
      System.out.println(read); 
      out.write(bytes, 0, read); 
     } 

     inputStream.close(); 
     out.flush(); 
     out.close(); 

     System.out.println("New file created!"); 
     } catch (IOException e) { 
     System.out.println(e.getMessage()); 
     } 

     } 

誰能幫助我在這裏?任何用於通過HTTP發送XML的示例客戶端/服務器示例都很棒。

+3

那麼* * *發生?是否拋出任何異常?如果你可以在你的問題中整理代碼,這真的會有所幫助 - 我認爲大約一半的服務器端行是註釋或只是空的,並且縮進也被破壞了... –

+0

我沒有得到任何異常,響應狀態碼是200,響應體是空白的。 – user966682

+0

那麼服務器端的日誌記錄會顯示什麼?你有沒有設法*讀*任何字節? –

回答

2

啊,發現它。看這裏:

OutputStream out = new FileOutputStream(new File("c:\\junaidAhmedJameel.xml")); 

這只是要寫入本地磁盤。您沒有向響應流寫入任何內容。您不清楚想要寫入響應流,但顯然不存在致電response.getWriter()response.getOutputStream()的呼叫。

您正在將內容長度設置爲100,但並未實際發送任何內容。請注意,硬編碼的內容長度幾乎肯定是做錯了反正......但它是肯定當你不發送任何內容時要做的錯誤...

+0

謝謝Skeet它得到了問題, – user966682

0

您永遠不會在您的服務器代碼中生成任何響應內容。你只需將長度設置爲100.

相關問題