[unity3d]unity跟.net進行http通訊

談談今天的學習感覺,今天收穫最大的就是解決了u3d向.net提交表單,而後.net服務器將接受過來的表單數據保存到sqlserver數據庫中。unity3d中wwwform默認的是post提交的。 php

http 提交數據原理 

http 協議經過 url來獲取和提交數據 。提交數據的方式 有兩種,一種是get方法,一種是post方法。get通常用於告訴服務器把知足參數的數據發送給回來。 html

例如:get 的html代碼以下: sql

[html]  view plain copy
  1. <form action="search.php" method ="GET">  
  2.     <username:<inputtypeinputtype="text"name="user"/><br>  
  3.     <password:<inputtypeinputtype="password "name="pwd"/><br>  
  4.      <input type="submit"value="login"/>  
  5. </form >  

post通常是將數據發送給服務器,服務器將這些數據進行處理,好比說存儲到數據庫。 數據庫

例如:post的html 代碼以下: 瀏覽器

[html]  view plain copy
  1. <form action="login.php" method ="POST" >  
  2.     <username:<inputtypeinputtype="text"name="user"/><br>  
  3.     <password:<inputtypeinputtype="password "name="pwd"/><br>  
  4.      <input type="submit"value="login"/>  
  5. </form >  

     其實區別就是提交的方式不同,點擊login按鈕後,瀏覽器地址欄裏分別顯示以下: 服務器

       get方法url爲:http://127.0.0.1/serach.php?user=hortor&pwd=123 sqlserver

       post方法url爲:http://127.0.0.1 post

客戶端發送表代碼:

[csharp]  view plain copy print ? 在CODE上查看代碼片 派生到個人代碼片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class test : MonoBehaviour {  
  5.   
  6.     private string url = "http://192.168.1.7/plusFile/Handler.ashx";  
  7.     private string urlname;  
  8.     void Start () {  
  9.         urlname = "丁小未";  
  10.           
  11.     }  
  12.       
  13.     void OnGUI()  
  14.     {  
  15.         GUILayout.Label("姓名:");  
  16.         urlname = GUILayout.TextField(urlname);  
  17.         if(GUILayout.Button("確認提交"))  
  18.         {  
  19.             StartCoroutine(myUpdate());   
  20.         }  
  21.     }  
  22.       
  23.     IEnumerator myUpdate()  
  24.     {  
  25.         WWWForm form = new WWWForm();  
  26.         form.AddField("url",urlname);  
  27.         WWW w = new WWW(url,form);  
  28.         yield return w;  
  29.         print(w.data);  
  30.         if(w.error!=null)  
  31.         {  
  32.             print("錯誤:"+w.error);  
  33.         }  
  34.         else  
  35.         {  
  36.             print("OK");  
  37.             print(w.text); //服務器端返回的數據  
  38.             print("長度:"+w.text.Length.ToString());  
  39.         }  
  40.     }  
  41. }  

效果圖:


服務器端接受代碼:

[csharp]  view plain copy print ? 在CODE上查看代碼片 派生到個人代碼片
  1. <%@ WebHandler Language="C#" Class="Handler" %>  
  2.   
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Data.SqlClient;  
  10. using System.Data;  
  11.   
  12. public class Handler : IHttpHandler {  
  13.       
  14.     public void ProcessRequest (HttpContext context) {  
  15.         context.Response.ContentType = "text/plain";  
  16.           
  17.         string name = context.Request.Form["url"];  
  18.         //string name = context.Request.QueryString["url"];  
  19.         if (name != null)  
  20.         {  
  21.             context.Response.Write("我接收到了:"+name);  
  22.             //context.Response.Write("<font color= 'red'>hello</font>");  
  23.             Test1 t = new Test1();  
  24.             t.conn(name);     
  25.         }  
  26.         else  
  27.         {  
  28.             context.Response.Write("error");  
  29.         }  
  30.     }  
  31.    
  32.     public bool IsReusable {  
  33.         get {  
  34.             return false;  
  35.         }  
  36.     }  
  37.   
  38. }  
  39.   
  40.   
  41. public class Test1  
  42. {  
  43.     SqlConnection dbConnection;  
  44.     private string sqlInsert;  
  45.     //private string name;  
  46.   
  47.     public Test1()  
  48.     {  
  49.           
  50.     }  
  51.   
  52.     public void conn(string name)  
  53.     {  
  54.         if (name != null)  
  55.         {  
  56.             sqlInsert = "INSERT INTO source(url) VALUES(" + "'"+name+"'" + ")";  
  57.             openSqlConnection();//打開數據庫  
  58.             doQuery(sqlInsert);  
  59.         }  
  60.     }  
  61.   
  62.     public void openSqlConnection()  
  63.     {  
  64.         dbConnection = new SqlConnection("server=.;database=Student;user id=sa;password=123456");  
  65.         dbConnection.Open();  
  66.     }  
  67.   
  68.     public void closeSqlConnection()  
  69.     {  
  70.         dbConnection.Close();  
  71.         dbConnection = null;  
  72.     }  
  73.   
  74.     public void doQuery(string strCommand)  
  75.     {  
  76.         SqlCommand dbCommand = dbConnection.CreateCommand();  
  77.         dbCommand.CommandText = strCommand;  
  78.         int i = dbCommand.ExecuteNonQuery();  
  79.         dbCommand.Dispose();  
  80.         dbCommand = null;  
  81.         if (i > 0)  
  82.         {  
  83.             //Response.Write("插入成功");  
  84.         }  
  85.     }  
  86. }  

服務器端效果圖:




技術討論羣:858550    歡迎前來討論技術問題。
相關文章
相關標籤/搜索