[unity3d]unity跟.net进行http通信

简介: 谈谈今天的学习感受,今天收获最大的就是解决了u3d向.net提交表单,然后.net服务器将接受过来的表单数据保存到sqlserver数据库中。unity3d中wwwform默认的是post提交的。

谈谈今天的学习感受,今天收获最大的就是解决了u3d向.net提交表单,然后.net服务器将接受过来的表单数据保存到sqlserver数据库中。unity3d中wwwform默认的是post提交的。

http 提交数据原理 

http 协议通过 url来获取和提交数据 。提交数据的方式 有两种,一种是get方法,一种是post方法。get一般用于告诉服务器把满足参数的数据发送给回来。

例如:get 的html代码如下:

[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

       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

需要工程文件的请留言!

相关文章
|
1月前
|
C# 图形学 C语言
Unity3D学习笔记3——Unity Shader的初步使用
Unity3D学习笔记3——Unity Shader的初步使用
34 0
|
3月前
|
图形学
【unity小技巧】unity3d创建和实现破碎打破物品,万物可破碎
【unity小技巧】unity3d创建和实现破碎打破物品,万物可破碎
95 0
【unity小技巧】unity3d创建和实现破碎打破物品,万物可破碎
|
3月前
|
图形学
【unity小技巧】unity3D寻路指示轨迹预测
【unity小技巧】unity3D寻路指示轨迹预测
51 0
|
3月前
|
JSON 开发框架 API
【推荐100个unity插件之20】一个强大的JSON处理库——Newtonsoft.Json(也称为Json.NET)
【推荐100个unity插件之20】一个强大的JSON处理库——Newtonsoft.Json(也称为Json.NET)
192 0
|
3月前
|
图形学
【unity小技巧】unity3d环境带雾的昼夜系统变化
【unity小技巧】unity3d环境带雾的昼夜系统变化
24 0
|
3月前
|
数据处理 图形学
【unity实战】unity3D中的PRG库存系统和换装系统(附项目源码)
【unity实战】unity3D中的PRG库存系统和换装系统(附项目源码)
30 0
|
3月前
|
开发工具 图形学 git
【实现100个unity特效之7】unity 3d实现各种粒子效果
【实现100个unity特效之7】unity 3d实现各种粒子效果
74 0
|
3月前
|
算法 图形学
【推荐100个unity插件之4】OpenFracture插件实现unity3d物体破裂和切割
【推荐100个unity插件之4】OpenFracture插件实现unity3d物体破裂和切割
38 0
|
3月前
|
API C# 图形学
【推荐100个unity插件之3】切割unity3d物体插件——Ezy-Slice的使用
【推荐100个unity插件之3】切割unity3d物体插件——Ezy-Slice的使用
68 0
|
4月前
|
JSON C# 图形学
【Unity 3D】利用C#、Unity和Socket实现简单的在线聊天室工具(附源码 简单易懂)
【Unity 3D】利用C#、Unity和Socket实现简单的在线聊天室工具(附源码 简单易懂)
110 0

热门文章

最新文章