Unity3D 网络通信_HTTP协议、处理Json格式返回值、请求加Oauth

时间:2023-01-05 18:56:31

Unity3D 网络通信_HTTP协议、处理Json格式返回值、请求加Oauth

post

Unity3D 网络通信_HTTP协议、处理Json格式返回值、请求加Oauth

get

Unity3D 网络通信_HTTP协议、处理Json格式返回值、请求加Oauth

登录

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Security.Authentication;
using LitJson;
using System;
using System.Globalization;
using System.Security.Cryptography;
using System.Web;

public static string url = "http://localhost:8080/film/";

public void Login(string username , string password)
{
string path = "/film/api/tqh/v2/member/login";
StartCoroutine(PostLogin(url+"api/tqh/v2/member/login?",path,username,password));
}
IEnumerator PostLogin(string url,string path,string username , string password){string input = "{\"email\":\""+username+"\",\"password\":\""+ToMd5(password)+"\"}";WWWForm form = new WWWForm();Hashtable headers = form.headers;byte[] rawData = Encoding.UTF8.GetBytes(input);headers["Content-Type"] = "application/json";headers["Accept"] = "application/json";DateTime date = DateTime.Now;string time =  date.ToString("ddd, yyyy-mm-dd HH':'mm':'ss 'UTC'",DateTimeFormatInfo.InvariantInfo);headers["Date"] = time;url = hmac(url,path,time);WWW www = new WWW(url, rawData, headers);yield return www;if (www.error != null){Debug.Log("error is login:"+ www.error  + "  " +input );camara.GetComponent<loginGUI>().loginfail(status_fail);} else{Debug.Log("request ok login: " + www.text);JsonData jd = JsonMapper.ToObject(www.text); string memberId = jd["memberId"].ToString();string bonusPoint = jd["bonusPoint"].ToString();string nickName = jd["nickName"].ToString();camara.GetComponent<loginGUI>().loginsuccess(memberId,nickName,bonusPoint,status_success);}}

oauth使用在headers中加入Date,date.ToString("ddd, yyyy-mm-dd HH':'mm':'ss 'UTC'",DateTimeFormatInfo.InvariantInfo)对日期的格式化

string time =  date.ToString("ddd, yyyy-mm-dd HH':'mm':'ss 'UTC'",DateTimeFormatInfo.InvariantInfo);

headers["Date"] = time;

www.text为后台返回的String字符串,是Json格式,但是不可以直接用key-value去取值,我们需要去转换使用LitJson,一个第三方插件

Unity3D 网络通信_HTTP协议、处理Json格式返回值、请求加Oauth

 
 

JsonData jd = JsonMapper.ToObject(www.text); 

string memberId = jd["memberId"].ToString();
string bonusPoint = jd["bonusPoint"].ToString();
string nickName = jd["nickName"].ToString();

在unity\项目\Assets\Plugins目录下引用LitJson,下载LitJson

Unity3D 网络通信_HTTP协议、处理Json格式返回值、请求加Oauth

之后就可以用key-value去取值了   

oauth的加入

public static  string API_KEY = "bc543dc89b***********";
public  static string SECURE_KEY = "32df2bc3520d53a1************";

public string hmac(string url ,string path ,string time){
StringBuilder stringToSign = new StringBuilder();
stringToSign.Append("POST").Append("\n");
stringToSign.Append(time).Append("\n");
stringToSign.Append(path).Append("\n");

var signature = string.Empty;
using (var hmac = new HMACSHA512(Encoding.UTF8.GetBytes(SECURE_KEY)))
{
var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToString()));
signature = Convert.ToBase64String(hash);
}

StringBuilder s = new StringBuilder();
s.Append(url);
s.Append("&apiKey=");
s.Append(HttpUtility.UrlEncode(API_KEY, Encoding.UTF8));
s.Append("&signature=");
s.Append(HttpUtility.UrlEncode(HttpUtility.UrlEncode(signature, Encoding.UTF8), Encoding.UTF8));

return s.ToString();
}


StringBuilder stringToSign = new StringBuilder();
stringToSign.Append("POST").Append("\n");
stringToSign.Append(time).Append("\n");
stringToSign.Append(path).Append("\n");

 

需要通过 httpMethod、time、和path(格式为"/film/api/tqh/v2/public/getAllHistoryRank")来获取HMACSHA512加密后的签名

var hmac = new HMACSHA512(Encoding.UTF8.GetBytes(SECURE_KEY))

对API_KEY和签名进行编码,签名编码两次是因为,在服务器被解码了两次一次jetty、一次后台

StringBuilder s = new StringBuilder();s.Append(url);s.Append("&apiKey=");s.Append(HttpUtility.UrlEncode(API_KEY, Encoding.UTF8));s.Append("&signature=");s.Append(HttpUtility.UrlEncode(HttpUtility.UrlEncode(signature, Encoding.UTF8), Encoding.UTF8));

HttpUtility使用需要引入的 using System.Web; 下载HttpUtility

Unity3D 网络通信_HTTP协议、处理Json格式返回值、请求加Oauth

Unity3D 网络通信_HTTP协议、处理Json格式返回值、请求加Oauth