我想从一个文档扫描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
'“错误 - 写入日志失败。”假设我们没有看你的电脑,并考虑添加'e.printStackTrace();'并复制/粘贴输出。 –
谢谢安德鲁。我已经添加了堆栈跟踪。这可能是我更感兴趣的来自服务器的东西 - 它看起来总是抛出异常以回应500错误。我需要知道为什么500错误被抛出。也许我需要getInputStream的替代方法,它仍然会查看输入流,而不会在500响应中抛出异常。 –
conn.getInputStream()失败,但conn.getErrorStream()从服务器返回我想要的输出。 (感谢http://stackoverflow.com/questions/3432263/java-io-ioexception-server-returned-http-response-code-500) –