2012-08-29 98 views
1

我想從一個文檔掃描applet的日誌數據發送到Django供電的Web服務器。http app之間的連接和Django服務器之間的post請求

我使用django自己的網絡服務器進行測試。我用java構建了一個簡單的POST請求,但是django服務器拋出了500錯誤。我無法獲得有關錯誤的任何信息。我似乎無法使用pydevd在我的django視圖中關閉代碼(我的url設置正確 - 如果我使用瀏覽器轉到url,則可以逐步執行代碼)。我的applet沒有任何調試設置,但是我有很多信息去控制檯。我知道django發送大量的html信息以及500錯誤,但是我的java applet在讀取消息之前會拋出異常(響應500錯誤)。

下面是我的小程序中的代碼 -

private boolean log(String scanURL) { 

    try { 
     String data = "log=" + buffer.toString(); 
     URL url = new URL(scanURL); 
     URLConnection conn = url.openConnection(); 
     conn.setDoOutput(true); 
     OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); 
     writer.write(data); 
     writer.flush(); 

     // Handle the response 
     int responseCode = ((HttpURLConnection) conn).getResponseCode(); 
     addItem("Http response code " + new Integer(responseCode).toString()); 
     addItem("creating BufferedReader"); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
     String line; 
     addItem("reading response"); 
     while ((line = reader.readLine()) != null) { 
      addItem(line); 
     } 
     addItem("Closing Writer"); 
     writer.close(); 
     addItem("Closing Reader"); 
     reader.close(); 

     if (responseCode == 200) { 
      return true; 
      } 
     else { 
      return false; 
     } 

    } catch (Exception e) { 
     addItem("Error - writing to log failed. " + e); 
     return false; 
    } 

} 

我的Django的看法是目前 -

from django.http import HttpResponse 
from django.contrib.auth.decorators import login_required 
from cache.shortcuts import permission_required 

@login_required 
@permission_required('documents.add_documentindex', raise_exception=True) 
def save_log(request): 
    import pydevd;pydevd.settrace() 
    log_text = request.POST['log'] 
    with open("scanning.log", "a") as f: 
     f.write(log_text) 
    return HttpResponse('ok') 

我懷疑我需要做的HTTP標頭的一些工作,但沒有多一點來自Django的信息對我的http知識是一個很大的障礙。

任何建議讓這個工作,甚至獲得更多的信息Django的服務器500錯誤將不勝感激!

UPDATE 從Java異常堆棧跟蹤如下:

java.io.IOException: Server returned HTTP response code: 500 for URL: http://10.0.0.68:8000/scanning_log 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source) 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
    at co.altcom.cache.scanner.CacheScan.log(CacheScan.java:408) 
    at co.altcom.cache.scanner.CacheScan.createLog(CacheScan.java:385) 
    at co.altcom.cache.scanner.CacheScan.destroy(CacheScan.java:70) 
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.io.IOException: Server returned HTTP response code: 500 for URL: http://10.0.0.68:8000/scanning_log 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
    at java.net.HttpURLConnection.getResponseCode(Unknown Source) 
    at co.altcom.cache.scanner.CacheScan.log(CacheScan.java:405) 
    ... 4 more 
+0

'「錯誤 - 寫入日誌失敗。」假設我們沒有看你的電腦,並考慮添加'e.printStackTrace();'並複製/粘貼輸出。 –

+0

謝謝安德魯。我已經添加了堆棧跟蹤。這可能是我更感興趣的來自服務器的東西 - 它看起來總是拋出異常以迴應500錯誤。我需要知道爲什麼500錯誤被拋出。也許我需要getInputStream的替代方法,它仍然會查看輸入流,而不會在500響應中拋出異常。 –

+0

conn.getInputStream()失敗,但conn.getErrorStream()從服務器返回我想要的輸出。 (感謝http://stackoverflow.com/questions/3432263/java-io-ioexception-server-returned-http-response-code-500) –

回答

2

我原來的問題問的建議得到從Django的服務器的詳細信息。我想我需要的答案在java.io.IOException: Server returned HTTP response code: 500找到。

Java的getInputStream當服務器返回錯誤代碼(例如500)時,URLConnection對象的方法拋出異常。解決方案是使用getErrorStream來代替。這讓我可以訪問django服務器產生的html錯誤信息。

我的小應用程序的日誌記錄方法的最後工作的代碼是 -

public boolean log(String logURL) { 

    String charset = "UTF-8"; 
    String logData = logBuffer.toString(); 
    OutputStream output = null; 
    HttpURLConnection conn = null; 
    BufferedReader reader = null; 
    InputStream in = null; 

    try { 
     String query = String.format("log=%s", URLEncoder.encode(logData, charset)); 
     conn = (HttpURLConnection) new URL(logURL).openConnection(); 
     conn.setDoOutput(true); 
     conn.setRequestProperty("Accept-Charset", charset); 
     conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=" + charset); 
     output = conn.getOutputStream(); 
     output.write(query.getBytes(charset)); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
     return false; 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
     return false; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } finally { 
     if (output != null) try { output.close(); } catch (IOException e) {e.printStackTrace();} 
    } 

    // Handle the response 
    try { 
     int responseCode = conn.getResponseCode(); 
     if (responseCode == 200) { 
      in = conn.getInputStream(); 
     } else { 
      in = conn.getErrorStream(); 
     } 
     reader = new BufferedReader(new InputStreamReader(in)); 
     String line; 
     logNote("reading response"); 
     while ((line = reader.readLine()) != null) { 
      System.out.println(line); 
     } 
     reader.close();   
     if (responseCode == 200) { 
      return true; 
      } 
     else { 
      return false; 
     } 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
     return false; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } finally { 
     if (reader != null) try { reader.close(); } catch (IOException e) {e.printStackTrace();} 
    } 
} 

Using java.net.URLConnection to fire and handle HTTP requests內容非常豐富。

希望這對別人有幫助。

相關問題