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 hands = new Dictionary();
        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 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);
        }
    }
}  

你可能感兴趣的:(c#)