谈谈今天的学习感受,今天收获最大的就是解决了u3d向.net提交表单,然后.net服务器将接受过来的表单数据保存到sqlserver数据库中。unity3d中wwwform默认的是post提交的。
http 提交数据原理
http 协议通过 url来获取和提交数据 。提交数据的方式 有两种,一种是get方法,一种是post方法。get一般用于告诉服务器把满足参数的数据发送给回来。
例如:get 的html代码如下:
- <form action="search.php" method ="GET">
- <username:<inputtypeinputtype="text"name="user"/><br>
- <password:<inputtypeinputtype="password "name="pwd"/><br>
- <input type="submit"value="login"/>
- </form >
post一般是将数据发送给服务器,服务器将这些数据进行处理,比如说存储到数据库。
例如:post的html 代码如下:
- <form action="login.php" method ="POST" >
- <username:<inputtypeinputtype="text"name="user"/><br>
- <password:<inputtypeinputtype="password "name="pwd"/><br>
- <input type="submit"value="login"/>
- </form >
其实区别就是提交的方式不一样,点击login按钮后,浏览器地址栏里分别显示如下:
get方法url为:http://127.0.0.1/serach.php?user=hortor&pwd=123
post方法url为:http://127.0.0.1
客户端发送表代码:
using UnityEngine; using System.Collections; public class test : MonoBehaviour { private string url = "http://192.168.1.7/plusFile/Handler.ashx"; private string urlname; void Start () { urlname = "丁小未"; } void OnGUI() { GUILayout.Label("姓名:"); urlname = GUILayout.TextField(urlname); if(GUILayout.Button("确认提交")) { StartCoroutine(myUpdate()); } } IEnumerator myUpdate() { WWWForm form = new WWWForm(); form.AddField("url",urlname); WWW w = new WWW(url,form); yield return w; print(w.data); if(w.error!=null) { print("错误:"+w.error); } else { print("OK"); print(w.text); //服务器端返回的数据 print("长度:"+w.text.Length.ToString()); } } }
效果图:
服务器端接受代码:
<%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; string name = context.Request.Form["url"]; //string name = context.Request.QueryString["url"]; if (name != null) { context.Response.Write("我接收到了:"+name); //context.Response.Write("<font color= 'red'>hello</font>"); Test1 t = new Test1(); t.conn(name); } else { context.Response.Write("error"); } } public bool IsReusable { get { return false; } } } public class Test1 { SqlConnection dbConnection; private string sqlInsert; //private string name; public Test1() { } public void conn(string name) { if (name != null) { sqlInsert = "INSERT INTO source(url) VALUES(" + "'"+name+"'" + ")"; openSqlConnection();//打开数据库 doQuery(sqlInsert); } } public void openSqlConnection() { dbConnection = new SqlConnection("server=.;database=Student;user id=sa;password=123456"); dbConnection.Open(); } public void closeSqlConnection() { dbConnection.Close(); dbConnection = null; } public void doQuery(string strCommand) { SqlCommand dbCommand = dbConnection.CreateCommand(); dbCommand.CommandText = strCommand; int i = dbCommand.ExecuteNonQuery(); dbCommand.Dispose(); dbCommand = null; if (i > 0) { //Response.Write("插入成功"); } } }
服务器端效果图:
==================== 迂者 丁小未 CSDN博客专栏=================
MyBlog:http://blog.csdn.net/dingxiaowei2013 MyQQ:1213250243
Unity QQ群:858550 cocos2dx QQ群:280818155
====================== 相互学习,共同进步 ===================
转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/17099057
欢迎关注我的微博:http://weibo.com/u/2590571922
需要工程文件的请留言!