3.为了演示通过Get方式请求数据,需要在网站中添加一个GetTest.aspx页面,其对应的ASPX.CS中的代码如下所示:
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;
public partial class GetTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//该代码功能是通过查询字符串CustomerID的值去查询该客户对应的公司名,
//并将查找到的公司返回给客户端
string customerid = this.Request.QueryString["CustomerID"];
if (customerid!=null )
{
string companyName = "";
using (SqlConnection con=new SqlConnection ("server=.;database=northwind;uid=sa;pwd=sa"))
{
var cmd = con.CreateCommand();
cmd.CommandText = "select companyname from customers where customerid=@customerid";
cmd.Parameters.AddWithValue("@customerid",customerid);
con.Open();
var result = cmd.ExecuteScalar();
if (result !=null )
{
companyName = result.ToString();
}
}
this.Response.Write(companyName);
this.Response.End();
}
}
}
4.为了演示通过POST方式发送数据,添加一个PostTest.aspx,其对应的CS文件代码如下所示:
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;
public partial class PostTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//该代码功能是通过获取客户端以Post方式发送的CustomerID的值去查询该客户//对应的公司名,并将查找到的公司返回给客户端
if (this.Request .ContentLength!=0)
{
//获取POST的数据流
var strm = this.Request.InputStream;
//建立缓冲区
byte[]buffer=new byte[this.Request .ContentLength];
//将POST过来的数据读取出来,并且存储在buffer中
strm.Read(buffer,0,buffer .Length );
//将二进制数据转化为字符串
string customerid = System.Text.Encoding.UTF8.GetString(buffer);
string companyName = "";
using (SqlConnection con = new SqlConnection("server=.;database=northwind;uid=sa;pwd=sa"))
{
var cmd = con.CreateCommand();
cmd.CommandText = "select companyname from customers where customerid=@customerid";
cmd.Parameters.AddWithValue("@customerid", customerid);
con.Open();
var result = cmd.ExecuteScalar();
if (result != null)
{
companyName = result.ToString();
}
}
this.Response.Write(companyName);
this.Response.End();
}
}
}
5.在Unity3D中建立一个新的项目,建立一个文件夹改名为Plugins,并将Warensoft.Unity.Communication.dll拷贝到该文件夹下面,如下图所示:
6.在Project中添加一个CS脚本,并将其命名为HttpTest.cs,其代码如下所示:
using UnityEngine;
using System.Collections;
using Warensoft.Unity.Communication.Client;
using System;
using System.Xml;
public class HttpTest : MonoBehaviour {
UnityHttpClient httpClient = null;
private Texture2D image;
void Start () {
//启动时将httpClient初始化
this.httpClient = UnityCommunicationManager.CreateInstance().GetHttpClient();
this.httpClient.Error += new EventHandler<HttpRequestErrorEventArgs>(httpClient_Error);
}
void httpClient_Error(object sender, HttpRequestErrorEventArgs e)
{
print(e.ResponseText );
}
int initStep = 0;
void Update () {
if (this.initStep ==0&&this.httpClient!=null )
{
//获取XML文件
this.httpClient.BeginGetHttpContent("http://localhost:17737/test11/test.xml",
//后面的方法是本次异步Http请求成功并响应后的回调方法
new Action<XmlDocument>((doc) =>
{
//打印XML文件
print("response xml content:");
print(doc.OuterXml);
}));
//获取图片
this.httpClient.BeginGetHttpContent("http://localhost:17737/test11/test.png",
//后面的方法是本次异步Http请求成功并响应后的回调方法
new Action<Texture2D>((img) =>
{
this.image = img;
}));
//获取纯文本
//通过客户ID查询公司名
//GET方式
this.httpClient.BeginGetHttpContent("http://localhost:17737/test11/GetTest.aspx?CustomerID=ALFKI",
//后面的方法是本次异步Http请求成功并响应后的回调方法
new Action<string>((stringResult) =>
{
//打印公司名
print("Get the company name of alfki:" + stringResult);
}));
//获取纯文本
//通过客户ID查询公司名
//POST方式
byte[] contentBuffer = System.Text.Encoding.UTF8.GetBytes("ALFKI");
this.httpClient.BeginPost("http://localhost:17737/test11/PostTest.aspx", contentBuffer,
(response) =>
{
//打印公司名
print("ost the company name of alfki:" + response.StringContent);
});
this.initStep = 1;
}
}
void OnGUI()
{
if (this.image !=null )
{
GUI.DrawTexture(new Rect (0,0,this.image .width,this.image.height),this.image);
}
}
}
7.将该cs文件拖放到该场景的主摄像机上,如下图所示: