title: Unable to complete HttpRequest - Error 500/403
body: <p>I am trying to post a <code>HttpRequest</code> from my android application to python based backend abd encountering 500 request code error.At times, it gives 403</p>
  
  <p><strong>App POST request</strong></p>
  
  <pre><code>private String Register(String email,String password) {
          HttpClient httpclient = new DefaultHttpClient();
          String responseStr="";
          String URL=Constants.URL;
          System.out.println(URL);
          HttpPost httppost = new HttpPost(URL);
  
          try {
              // Add your data
              InetAddress ipAddress=null;
              try{
                  ipAddress=InetAddress.getLocalHost();
                  System.out.println("IP of my Android := "+ipAddress.getHostAddress());
              }catch (Exception e){
                  System.out.println("Exception caught ="+e.getMessage());
              }
              List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
              nameValuePairs.add(new BasicNameValuePair("email", email));
              nameValuePairs.add(new BasicNameValuePair("password", password));
              nameValuePairs.add(new BasicNameValuePair("ip", ipAddress.getHostAddress().toString()));
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
              System.out.println(email+","+password+","+ipAddress.getHostAddress().toString()+","+URL);
              // Execute HTTP Post Request
              HttpResponse response = httpclient.execute(httppost);
  
              int responseCode = response.getStatusLine().getStatusCode();
              System.out.println("after this"+responseCode);
              switch(responseCode) {
              case 200:
                  HttpEntity entity = response.getEntity();
                  if(entity != null) {
                      String responseBody = EntityUtils.toString(entity);
                      responseStr=responseBody;
                  }
                  break;
              }
          } catch (Exception e) {
              e.printStackTrace();
              Toast.makeText(Registration.this,"error", 
                      Toast.LENGTH_SHORT).show();
              //Constants.AlertBox("Failed", "Unable to register. Please try again", Registration.this);
          } 
          return responseStr;
      }
  </code></pre>
  
  <p><strong>backend</strong></p>
  
  <pre><code>@csrf_exempt       
  def registration(request):
      registration_dict = {}
      #if 1==1 :
      if request.POST:
          email=request.POST['email']
          password=request.POST['password']
          ip=request.POST['ip']
          location=request.POST['location']
          #email="admin@admin.com"
          #password='123456'
          #ip='203.192.223.115'
          user = auth.authenticate(username=email, password=password)
          if user is None:
              user = User(username=email, password=password, email=email)
              user = UserInfo.objects.get_or_create(user=user, email_id=email)
              registration_dict["status"]="1"
              registration_dict["message"]="Thank You for registering"
              now=datetime.now()
              UserHistory.objects.create(user=user,location=location,authentication=1,date=now)
          else:
              registration_dict["status"]="2"
              registration_dict["message"]="User already registered"
  
  
          return HttpResponse(simplejson.dumps(registration_dict),content_type="application/json") 
      else:
          registration_dict["status"]="0"
          registration_dict["message"]="Unable to process the request"
          return HttpResponse(simplejson.dumps(registration_dict),content_type="application/json")
  </code></pre>
  
  <p><strong>LogCat Response</strong></p>
  
  <pre><code>07-30 18:17:10.459: I/System.out(9538):           &lt;code&gt;/Library/Python/2.6/site-packages/django/core/handlers/base.py&lt;/code&gt; in &lt;code&gt;get_response&lt;/code&gt;
  07-30 18:17:10.459: I/System.out(9538):                 &lt;ol start="104" class="pre-context" id="pre4328052856"&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                    for middleware_method in self._view_middleware:&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        response = middleware_method(request, callback, callback_args, callback_kwargs)&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        if response:&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                            break&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                if response is None:&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                    try:&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;
  07-30 18:17:10.459: I/System.out(9538):               &lt;ol start="111" class="context-line"&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        response = callback(request, *callback_args, **callback_kwargs)&lt;/pre&gt; &lt;span&gt;...&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;
  07-30 18:17:10.459: I/System.out(9538):                 &lt;ol start='112' class="post-context" id="post4328052856"&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                    except Exception, e:&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        # If the view raised an exception, run it through exception&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        # middleware, and if the exception middleware returns a&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        # response, use that. Otherwise, reraise the exception.&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        for middleware_method in self._exception_middleware:&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                            response = middleware_method(request, e)&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;
  07-30 18:17:10.469: I/System.out(9538):                     &lt;td&gt;response&lt;/td&gt;
  07-30 18:17:10.529: I/System.out(9538): File "/Library/Python/2.6/site-packages/django/core/handlers/base.py" in get_response
  07-30 18:17:10.529: I/System.out(9538):   111.                         response = callback(request, *callback_args, **callback_kwargs)
  07-30 18:17:10.709: I/System.out(9538): this is login response 
  07-30 18:17:10.749: I/System.out(9538):           &lt;code&gt;/Library/Python/2.6/site-packages/django/core/handlers/base.py&lt;/code&gt; in &lt;code&gt;get_response&lt;/code&gt;
  07-30 18:17:10.749: I/System.out(9538):                 &lt;ol start="104" class="pre-context" id="pre4328052856"&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                    for middleware_method in self._view_middleware:&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        response = middleware_method(request, callback, callback_args, callback_kwargs)&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        if response:&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                            break&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                if response is None:&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                    try:&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;
  07-30 18:17:10.749: I/System.out(9538):               &lt;ol start="111" class="context-line"&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        response = callback(request, *callback_args, **callback_kwargs)&lt;/pre&gt; &lt;span&gt;...&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;
  07-30 18:17:10.749: I/System.out(9538):                 &lt;ol start='112' class="post-context" id="post4328052856"&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                    except Exception, e:&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        # If the view raised an exception, run it through exception&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        # middleware, and if the exception middleware returns a&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        # response, use that. Otherwise, reraise the exception.&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                        for middleware_method in self._exception_middleware:&lt;/pre&gt;&lt;/li&gt;&lt;li onclick="toggle('pre4328052856', 'post4328052856')"&gt;&lt;pre&gt;                            response = middleware_method(request, e)&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;
  07-30 18:17:10.749: I/System.out(9538):                     &lt;td&gt;response&lt;/td&gt;
  07-30 18:17:10.759: I/System.out(9538): File "/Library/Python/2.6/site-packages/django/core/handlers/base.py" in get_response
  07-30 18:17:10.759: I/System.out(9538):   111.                         response = callback(request, *callback_args, **callback_kwargs)
  </code></pre>