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

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

post


get


登录

[csharp]  view plain  copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5. using System.Security.Cryptography;  
  6. using System.Security.Authentication;  
  7. using LitJson;  
  8. using System;  
  9. using System.Globalization;   
  10. using System.Security.Cryptography;  
  11. using System.Web;  
  12.   
  13. public static string url = "http://localhost:8080/film/";  
  14.   
  15. public void Login(string username , string password)  
  16.     {     
  17.         string path = "/film/api/tqh/v2/member/login";  
  18.         StartCoroutine(PostLogin(url+"api/tqh/v2/member/login?",path,username,password));         
  19.     }  
[csharp]  view plain  copy
  1. IEnumerator PostLogin(string url,string path,string username , string password)  
  2.     {  
  3.         string input = "{\"email\":\""+username+"\",\"password\":\""+   ToMd5(password)+"\"}";  
  4.         WWWForm form = new WWWForm();         
  5.         Hashtable headers = form.headers;  
  6.         byte[] rawData = Encoding.UTF8.GetBytes(input);  
  7.         headers["Content-Type"] = "application/json";  
  8.         headers["Accept"] = "application/json";  
  9.   
  10.         DateTime date = DateTime.Now;  
  11.         string time =  date.ToString("ddd, yyyy-mm-dd HH':'mm':'ss 'UTC'",DateTimeFormatInfo.InvariantInfo);  
  12.         headers["Date"] = time;  
  13.         url = hmac(url,path,time);  
  14.   
  15.         WWW www = new WWW(url, rawData, headers);  
  16.         yield return www;         
  17.         if (www.error != null)  
  18.         {  
  19.             Debug.Log("error is login:"+ www.error  + "  " +input );      
  20.             camara.GetComponent<loginGUI>().loginfail(status_fail);  
  21.         } else  
  22.         {  
  23.             Debug.Log("request ok login: " + www.text);  
  24.   
  25.             JsonData jd = JsonMapper.ToObject(www.text);   
  26.   
  27.             string memberId = jd["memberId"].ToString();  
  28.             string bonusPoint = jd["bonusPoint"].ToString();  
  29.             string nickName = jd["nickName"].ToString();  
  30.             camara.GetComponent<loginGUI>().loginsuccess(memberId,nickName,bonusPoint,status_success);  
  31.         }  
  32.     }  

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,一个第三方插件


 
 

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


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

oauth的加入

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

[csharp]  view plain  copy
  1. public string hmac(string url ,string path ,string time){  
  2.     StringBuilder stringToSign = new StringBuilder();  
  3.     stringToSign.Append("POST").Append("\n");  
  4.     stringToSign.Append(time).Append("\n");  
  5.     stringToSign.Append(path).Append("\n");   
  6.   
  7.     var signature = string.Empty;  
  8.     using (var hmac = new HMACSHA512(Encoding.UTF8.GetBytes(SECURE_KEY)))  
  9.     {  
  10.         var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToString()));  
  11.         signature = Convert.ToBase64String(hash);  
  12.     }  
  13.   
  14.     StringBuilder s = new StringBuilder();  
  15.     s.Append(url);  
  16.     s.Append("&apiKey=");  
  17.     s.Append(HttpUtility.UrlEncode(API_KEY, Encoding.UTF8));  
  18.     s.Append("&signature=");  
  19.     s.Append(HttpUtility.UrlEncode(HttpUtility.UrlEncode(signature, Encoding.UTF8), Encoding.UTF8));  
  20.   
  21.     return s.ToString();  
  22. }  


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


                
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值