zoukankan      html  css  js  c++  java
  • Unity的http通信--unity与python的django通信

    http://blog.csdn.net/chenggong2dm/article/details/17372203

    写在前面:

    WWW类,是unity里,简单的访问网页的类。本文介绍的就是这种方式,与web服务器之间进行通信。当然,HTTP通信,也可以自己通过socket去写,自己实现一个http通信。

    WWW类可以用来发送GET和POST请求到服务器,WWW类默认使用GET方法,并且如果提供一个postData参数可用POST方法。这里我们主要使用实用性更强一些的POST方式。

    WWW的完整构造函数如下:

    WWWurl:string, postData:byte[], headers:Hashtable )

    • url
      The url to download.
    • postData
      A byte array of data to be posted to the url.
    • headers
      A hash table of custom headers to send with the request.

    ■注意:这个构造函数,有函数重载,可以省略第三个headers参数,也就是:

    WWWurl:string, postData:byte[] )


    实际例子:

    1,新建一个空项目。【File】-->【New Project】

    2,新建一个2D背景,用于衬托UI。【GameObject】-->【CreateOther】-->【GUI Texture】

    3,写HttpTest.cs脚本文件,绑定到摄像机上。代码如下:


    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
    1. using UnityEngine;  
    2. using System.Collections;  
    3.   
    4. public class HttpTest : MonoBehaviour {  
    5.     //variables  
    6.     public string str_uid = "";  
    7.     public string str_score = "";  
    8.     public string str_response = "";  
    9.       
    10.     // Use this for initialization  
    11.     void Start () {  
    12.       
    13.     }  
    14.       
    15.     // Update is called once per frame  
    16.     void Update () {  
    17.           
    18.     }  
    19.       
    20.     //在C#中, 需要用到yield的话, 必须建立在IEnumerator类中执行  
    21.     IEnumerator TestPost()  
    22.     {  
    23.         //WWW的三个参数: url, postData, headers  
    24.         string url = "http://127.0.0.1/test/";  
    25.         byte[] post_data;  
    26.         Hashtable headers;   //System.Collections.Hashtable  
    27.           
    28.         string str_params;  
    29.         str_params = "uid=" + str_uid + "&" + "score=" + str_score;   
    30.         post_data = System.Text.UTF8Encoding.UTF8.GetBytes(str_params);  
    31.         //Encoding encode = System.Text.Encoding.GetEncoding("utf-8");  
    32.         //byte[] post_data = encode.GetBytes("uid=中文&score=100");  
    33.         headers = new Hashtable();  
    34.         //headers.Add("Content-Type","application/x-www-form-urlencoded");  
    35.         headers.Add("CONTENT_TYPE""text/plain");  
    36.           
    37.         //发送请求  
    38.         WWW www_instance = new WWW(url, post_data, headers);  
    39.           
    40.         //web服务器返回  
    41.         yield return www_instance;  
    42.         if (www_instance.error != null)  
    43.         {  
    44.             Debug.Log(www_instance.error);  
    45.         }  
    46.         else  
    47.         {  
    48.             this.str_response = www_instance.text;  
    49.         }   
    50.     }  
    51.       
    52.     void OnGUI () {  
    53.         GUI.Label(new Rect(10,20,60,20),"UID:   ");   
    54.         GUI.Label(new Rect(10,45,60,20),"Score: ");   
    55.         //注意:因为每一帧都在刷, 所以[文本框]是这种写法:  
    56.         str_uid = GUI.TextField(new Rect(60, 20, 160, 20), str_uid);  
    57.         str_score = GUI.TextField(new Rect(60, 45, 160, 20), str_score);  
    58.           
    59.         //发送Http的POST请求  
    60.         if (GUI.Button(new Rect(120,80,100,25),"发送请求"))  
    61.         {  
    62.             StartCoroutine(TestPost());  
    63.         }  
    64.           
    65.         this.str_response = GUI.TextArea(new Rect(10, 150, 210, 100), this.str_response);   
    66.     }  
    67. }  

    4,运行。效果如下:



    5,点击,发送POST请求,并显示服务器返回的结果:



    附注A:

    下面是对应的web服务器端代码:

    本例的web服务器,使用的是python的django,使用方法可以参加我上一篇文章:【新版django1.6的Hello world】

    views代码如下:

    [python] view plaincopy在CODE上查看代码片派生到我的代码片
    1. #! /usr/bin/env python  
    2. #coding=utf-8  
    3.   
    4. from django.http import HttpResponse  
    5.   
    6. def test_post(request):  
    7.     fanhui = u'服务器返回: ' + u'用户UI:'+ unicode(request.POST['uid']) +' '  
    8.     fanhui = fanhui + u'分数:'+ unicode(request.POST['score'])  
    9.     return HttpResponse(fanhui)  


    附注B:

    如果使用django,注意要把中间件里的:

    'django.middleware.csrf.CsrfViewMiddleware',        注释掉。否则请求会因为CSRF机制,给拦下,报403错误。

    或者干脆禁用中间件,也行。

  • 相关阅读:
    python 的基础 学习 第六天 基础数据类型的操作方法 字典
    python 的基础 学习 第五天 基础数据类型的操作方法
    python 的基础 学习 第四天 基础数据类型
    ASP.NET MVC 入门8、ModelState与数据验证
    ASP.NET MVC 入门7、Hellper与数据的提交与绑定
    ASP.NET MVC 入门6、TempData
    ASP.NET MVC 入门5、View与ViewData
    ASP.NET MVC 入门4、Controller与Action
    ASP.NET MVC 入门3、Routing
    ASP.NET MVC 入门2、项目的目录结构与核心的DLL
  • 原文地址:https://www.cnblogs.com/nafio/p/9137512.html
Copyright © 2011-2022 走看看