Reputation: 13308
I'm trying to send logging data from a document scanning applet to a django powered web server.
I'm testing using django's own webserver. I've constructed a simple POST request with java, but the django server is throwing a 500 error. I can't get any information regarding the error. I can't seem to use pydevd to break on code in my django view (my url's are set correctly - I can step through the code if I go to the url with a browser). I don't have any debugging setup on my applet, but I do have lots of info going to the console. I know that django sends lots of html info along with a 500 error, but my java applet is throwing an exception (in response to the 500 error), before it can read the message.
Here's the code in my applet -
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;
}
}
And my django view is currently -
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')
I suspect I need to do some work on the http headers, but without a bit more info from django my knowledge of http is a big handicap.
Any suggestions for getting this to work, or even getting a bit more information on django's server 500 error would be greatly appreciated!
UPDATE The stacktrace from the java exception is as follows
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
Upvotes: 1
Views: 2057
Reputation: 13308
My original question asked for suggestions to get more information from the django server. I guess the answer I needed was found at java.io.IOException: Server returned HTTP response code: 500.
Java's getInputStream
method of a URLConnection object throws an exception when the server returns an error code (such as 500). The solution was to use getErrorStream instead. This allowed me to access the html error information produced by django's server.
The final working code for the logging method of my applet is -
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 was very informative.
Hope this is helpful for someone else.
Upvotes: 2