2012-08-29 96 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内容非常丰富。

希望这对别人有帮助。

相关问题