title: http connection between java applet and django server for post request
body: <p>I'm trying to send logging data from a document scanning applet to a django powered web server. </p>
  
  <p>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.</p>
  
  <p>Here's the code in my applet - </p>
  
  <pre><code>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;
      }
  
  }
  </code></pre>
  
  <p>And my django view is currently - </p>
  
  <pre><code>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')
  </code></pre>
  
  <p>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.</p>
  
  <p>Any suggestions for getting this to work, or even getting a bit more information on django's server 500 error would be greatly appreciated!</p>
  
  <p>UPDATE
  The stacktrace from the java exception is as follows</p>
  
  <pre><code>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
  </code></pre>