Android服务器django,Android发送发送请求到django服务器csrf失败
我想我的
Android應用程序能夠發送一些信息到我的django服務器.所以我做的Android應用程序發送一個發布請求到mysite /上傳頁面,django的這個頁面的視圖將基于post數據工作.問題是服務器對post請求的響應,關于csrf驗證失敗.看看這個問題,似乎我可能必須先從服務器獲取一個csrf令牌,然后用該令牌做帖子但是我不知道我該怎么做.編輯:我已經發現,我可以使用視圖裝飾器@csrf_exempt敲擊crsf驗證這個視圖,但我不知道這是否是最好的解決方案.我的Android代碼:
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
// Add your data
List nameValuePairs = new ArrayList(2);
nameValuePairs.add(new BasicNameValuePair("scoreone",scoreone));
nameValuePairs.add(new BasicNameValuePair("scoretwo",scoretwo));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
System.out.println("huzahhhhhhh");
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
System.out.println("Result: "+result);
和我的視圖代碼來處理上傳:
# uploads a players match
def upload(request):
if request.method == 'POST':
scoreone = int(request.POST['scoreone'])
scoretwo = int(request.POST['scoretwo'])
m = Match.objects.create()
MatchParticipant.objects.create(player = Player.objects.get(pk=1),match = m,score = scoreone)
MatchParticipant.objects.create(player = Player.objects.get(pk=2),score = scoretwo)
return HttpResponse("Match uploaded" )
enter code here
總結
以上是生活随笔為你收集整理的Android服务器django,Android发送发送请求到django服务器csrf失败的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android listview edi
- 下一篇: win10系统GHOST备份方法是什么