参考:
https://blog.csdn.net/mseol/article/details/54138762
https://blog.csdn.net/h570768995/article/details/50386935
https://stackoverflow.com/questions/16910788/unity3d-post-a-json-to-asp-net-mvc-4-web-api
图片和base64互转https://blog.csdn.net/jiangyangll/article/details/80566330
C# int和byte之间的互转https://blog.csdn.net/zuoyefeng1990/article/details/71480499
短链接使用WWW或UnityWebRequest发送请求
WWW的构造函数有3种:
1.只发送http请求,不带参数
public WWW(string url);
2.使用表单发送带数据的http请求
public WWW(string url, WWWForm form);
public WWW(string url, byte[] postData);// 这个数据头默认好像是表单
3.使用json发送带数据的http请求
public WWW(string url, byte[] postData, Dictionary<string, string> headers);
json转byte[] 代码:
System.Text.Encoding.UTF8.GetBytes(json.ToString())
json数据表头:
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
附1:
long数据转byte[]
public static byte[] longToBytes(long value)
{
byte[] src = new byte[8];
src[7] = (byte)((value >> 56) & 0xFF);
src[6] = (byte)((value >> 48) & 0xFF);
src[5] = (byte)((value >> 40) & 0xFF);
src[4] = (byte)((value >> 32) & 0xFF);
src[3] = (byte)((value >> 24) & 0xFF);
src[2] = (byte)((value >> 16) & 0xFF);
src[1] = (byte)((value >> 8) & 0xFF);
src[0] = (byte)(value & 0xFF);
return src;
}
附2:
返回的数据解析成Json(使用SimpleJson)
object obj = null;
bool success = SimpleJson.SimpleJson.TryDeserializeObject(www.text, out obj);
if (!success || obj == null)
{
Debug.LogError("Network Error: Http Response is not Json!");
yield break;
}
JsonObject args = obj as JsonObject;
附3:
base64图片数据生成图片
public static void Base64ToImg(Image imgComponent, string base64, int width, int height)
{
byte[] bytes = System.Convert.FromBase64String(base64);
Texture2D tex2D = new Texture2D(width, height);
tex2D.LoadImage(bytes);
Sprite s = Sprite.Create(tex2D, new Rect(0, 0, tex2D.width, tex2D.height), new Vector2(0.5f, 0.5f));
imgComponent.sprite = s;
Resources.UnloadUnusedAssets();
}
其他