// Create a new httpClIEnt and Post header httpClIEnt httpclIEnt = new DefaulthttpClIEnt(); httpPost httppost = new httpPost(URL); // Add your data List<nameValuePair> nameValuePairs = new ArrayList<nameValuePair>(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 matchdef 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解决方法 编写自己的装饰器,并在您的请求中添加一些“秘密”标题. https://code.djangoproject.com/browser/django/trunk/django/views/decorators/csrf.py
def csrf_exempt(vIEw_func): """ Marks a vIEw function as being exempt from the CSRF vIEw protection. """ # We Could just do vIEw_func.csrf_exempt = True,but decorators # are nicer if they don't have sIDe-effects,so we return a new # function. def wrapped_vIEw(request,*args,**kwargs): return vIEw_func(request,**kwargs) if request.Meta.has_key('http_X_SKIP_CSRF'): wrapped_vIEw.csrf_exempt = True return wraps(vIEw_func,assigned=available_attrs(vIEw_func))(wrapped_vIEw)总结
以上是内存溢出为你收集整理的Android发送发送请求到django服务器csrf失败全部内容,希望文章能够帮你解决Android发送发送请求到django服务器csrf失败所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)