我正在尝试将HttpRequest
我的 android 应用程序中的一个发布到基于 python 的后端 abd 遇到 500 请求代码错误。有时,它会给出 403
应用 POST 请求
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<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
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;
}
后端
@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")
LogCat 响应
07-30 18:17:10.459: I/System.out(9538): <code>/Library/Python/2.6/site-packages/django/core/handlers/base.py</code> in <code>get_response</code>
07-30 18:17:10.459: I/System.out(9538): <ol start="104" class="pre-context" id="pre4328052856"><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> for middleware_method in self._view_middleware:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> response = middleware_method(request, callback, callback_args, callback_kwargs)</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> if response:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> break</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre></pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> if response is None:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> try:</pre></li></ol>
07-30 18:17:10.459: I/System.out(9538): <ol start="111" class="context-line"><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> response = callback(request, *callback_args, **callback_kwargs)</pre> <span>...</span></li></ol>
07-30 18:17:10.459: I/System.out(9538): <ol start='112' class="post-context" id="post4328052856"><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> except Exception, e:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> # If the view raised an exception, run it through exception</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> # middleware, and if the exception middleware returns a</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> # response, use that. Otherwise, reraise the exception.</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> for middleware_method in self._exception_middleware:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> response = middleware_method(request, e)</pre></li></ol>
07-30 18:17:10.469: I/System.out(9538): <td>response</td>
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): <code>/Library/Python/2.6/site-packages/django/core/handlers/base.py</code> in <code>get_response</code>
07-30 18:17:10.749: I/System.out(9538): <ol start="104" class="pre-context" id="pre4328052856"><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> for middleware_method in self._view_middleware:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> response = middleware_method(request, callback, callback_args, callback_kwargs)</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> if response:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> break</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre></pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> if response is None:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> try:</pre></li></ol>
07-30 18:17:10.749: I/System.out(9538): <ol start="111" class="context-line"><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> response = callback(request, *callback_args, **callback_kwargs)</pre> <span>...</span></li></ol>
07-30 18:17:10.749: I/System.out(9538): <ol start='112' class="post-context" id="post4328052856"><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> except Exception, e:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> # If the view raised an exception, run it through exception</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> # middleware, and if the exception middleware returns a</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> # response, use that. Otherwise, reraise the exception.</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> for middleware_method in self._exception_middleware:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre> response = middleware_method(request, e)</pre></li></ol>
07-30 18:17:10.749: I/System.out(9538): <td>response</td>
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)