HTTP传输

服务器

因为HTTP传输是JSON类型的数据,所以加载个JSON包
//定义委托

using LitJson;
public delegate string MsgHand(JsonData msg);

自带的 ValuesController 只需要在Post方式里面加读取数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Swashbuckle.Swagger.Annotations;
using LitJson;
    public class ValuesController : ApiController
    {
        Dictionary<int, MsgHand> hands = new Dictionary<int, MsgHand>();
        protected ValuesController()
        {
            init();
        }
        void init()
        {
            LoginModels login = new LoginModels();
            hands.Add(10001, login.Login);
            hands.Add(10002, login.Register);
        }
        // GET api/values
        [SwaggerOperation("GetAll")]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
        // GET api/values/5
        [SwaggerOperation("GetById")]
        [SwaggerResponse(HttpStatusCode.OK)]
        [SwaggerResponse(HttpStatusCode.NotFound)]
        public string Get(int id)
        {
            return "value";
        }
        // POST api/values
        [SwaggerOperation("Create")]
        [SwaggerResponse(HttpStatusCode.Created)]
        public string Post([FromBody]string value)
        {
            JsonData data = LitJson.JsonMapper.ToObject(value);
            int pro = int.Parse(data["pro"].ToString());
            MsgHand hand;
            hands.TryGetValue(pro, out hand);
            if (hand != null)
                return hand(data);
            return null;
        }
        // PUT api/values/5
        [SwaggerOperation("Update")]
        [SwaggerResponse(HttpStatusCode.OK)]
        [SwaggerResponse(HttpStatusCode.NotFound)]
        public void Put(int id, [FromBody]string value)
        {
        }
        // DELETE api/values/5
        [SwaggerOperation("Delete")]
        [SwaggerResponse(HttpStatusCode.OK)]
        [SwaggerResponse(HttpStatusCode.NotFound)]
        public void Delete(int id)
        {
        }
    }
如果是登录的话,判断数据,再把结果发出去
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LitJson;
public class LoginModels
{
    public string Login(JsonData msg)
    {
        JsonData data = new JsonData();
        data["pro"] = "10001";
        if (msg["id"].ToString() == "123" && msg["pa"].ToString() == "123")
        {
            data["res"] = "true";
        }
        else
        {
            data["res"] = "false";
        }
        return data.ToJson();
    }
}
客户端

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
public class HTTPClient : MonoBehaviour
{
    void Start()
    {
        JsonData data = new JsonData();
        data["pro"] = "10001";
        data["id"] = "123";
        data["pa"] = "123";
        string msg = data.ToJson();
        StartCoroutine(SendMsg(msg));
    }
    IEnumerator SendMsg(string str)
    {
        WWWForm from = new WWWForm();
        from.AddField("", str);
        WWW www = new WWW("http://localhost:10086/api/values", from);
        yield return www;
        if (www.error == null)
        {
            if (www.text != null)
            {
                Debug.Log(www.text);
                string st = www.text.Replace("\\", "");
                Debug.Log(st);
                st= st.Remove(0, 1);
                st= st.Remove(st.Length - 1, 1);
                JsonData data = JsonMapper.ToObject(st);
                Debug.Log(data["pro"].ToString());
            }
            else
            {
                Debug.Log("不知道遇到什么问题");
            }
        }
        else
        {
            Debug.Log(www.error);
        }
    }
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值