Unity 中通过HttpWebRequest以POST形式传JSON格式(键值对格式)的参数请求数据。
注意:处理HttpWebRequest访问https有安全证书的问题( 请求被中止: 未能创建 SSL/TLS 安全通道。)
只需加上以下两行代码就行了。
ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
POST请求实现以下要求
POST "https://....."; //提供获取Token值的服务接口地址(很重要)
入参body(json格式):
{
"username":"后端提供",
"password":"后端提供",
"clientType": "WEB",
"pid":"01967189376"
}
/// <summary>
/// POST请求方法
/// </summary>
/// <param name="url">获取Token值的服务URL地址(很重要)</param>
/// <param name="jsonData">传入请求的参数,此处参数为JOSN格式</param>
public void HttpPost(string url, string jsonData)
{
//处理HttpWebRequest访问https有安全证书的问题( 请求被中止: 未能创建 SSL/TLS 安全通道。)
ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
byte[] byteArray = Encoding.UTF8.GetBytes(jsonData);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json";
//httpWebRequest.ContentLength = byteArray.Length; //此处注释的情况下需要调整“myResponseStream.Close();"执行顺序
Stream myResponseStream = httpWebRequest.GetRequestStream();
myResponseStream.Write(byteArray, 0, byteArray.Length);
myResponseStream.Close(); //"httpWebRequest.ContentLength"未赋值的情况下需要在此处执行关闭语句;若赋值了,可以放在最后执行关闭语句---
HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
response.Close();
if (!string.IsNullOrEmpty(retString))
{
Debug.LogError(retString);
TokenDataInfo tokenDataInfo = JsonMapper.ToObject<TokenDataInfo>(retString);
authorization = tokenDataInfo.token_type + " " + tokenDataInfo.access_token;
Debug.Log(authorization);
}
}
Unity 中通过HttpWebRequest 以GET形式传authorization 的参数请求数据。
注意:处理HttpWebRequest访问https有安全证书的问题( 请求被中止: 未能创建 SSL/TLS 安全通道。)
只需加上以下两行代码就行了。
ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
GET请求实现以下要求
GET "https://....."; //提供请求数据的URL地址,以头文件形式发起请求进行Token验证
请求头带上
authorization
bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI3MDE1MTQ0NzM0NSIsInBpZCI6IjAxOTY3MTg5Mzc2IiwiZW50IjoiMDE5NjcxODkzNzYiLCJ0eXBlIjoiV0VCIiwiZXhwIjoxNTYwNjA2MjIwLCJqdGkiOiJkNDlhZWFkMi1lM2FhLTQ5MGItYWZiMC1iNmVmNDNmMTY5Y2UiLCJyb2xlcyI6W119.Vdumf8OOcLnfu8i2KiP7qzN1nPmSXmcwnH74LKFsydImgcVIBMT9rV4zCz8UnJxQq55XIQr1IjiAe1cIYEtESw==
/// <summary>
/// GET请求
/// ContentType = "application/json";
/// token为Authorization中的授权验证码
/// </summary>
/// <param name="Url">请求数据的URL地址</param>
/// <param name="token">token为Authorization中的授权验证码</param>
/// <returns></returns>
public void HttpGet(string url, string token)
{
//处理HttpWebRequest访问https有安全证书的问题( 请求被中止: 未能创建 SSL/TLS 安全通道。)
ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
//request.ContentType = "application/json";
if (authorization != null)
{
request.Headers.Add("authorization", token);//请求头文件
Debug.LogError(authorization);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
response.Close();
if (!string.IsNullOrEmpty(retString))
{
Debug.LogError(retString);
GetDataInfo getDataInfo = JsonMapper.ToObject<GetDataInfo>(retString);
Debug.LogError(getDataInfo.address["name"]);
Debug.LogError(getDataInfo.companyInfo["companyName"]);
Debug.LogError(getDataInfo.alarmCount);
Debug.LogError(getDataInfo.faultCount);
}
}
}