Skip to content

Instantly share code, notes, and snippets.

@kurokuru
Created May 16, 2019 13:51
Show Gist options
  • Save kurokuru/3501ab8cc14338d09304bf826fc8628e to your computer and use it in GitHub Desktop.
Save kurokuru/3501ab8cc14338d09304bf826fc8628e to your computer and use it in GitHub Desktop.
GAEにUnityWebRequestでgzipする
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using Ionic.Zlib;
public class WebRequest : MonoBehaviour
{
// Start is called before the first frame update
IEnumerator Start()
{
var request = UnityWebRequest.Get("{your app engine url hare}");
request.SetRequestHeader("accept-encoding", "gzip");
request.SetRequestHeader("user-agent", "gzip");
yield return request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
Debug.LogError(request.error);
yield break;
}
var data = request.downloadHandler.data;
var text = ""; // 解答されたテキスト
if (data[0] == 0x1f && data[1] == 0x8b)
{ // gzip
text = System.Text.Encoding.UTF8.GetString(GZipStream.UncompressBuffer(data));
}
else
{ // plane data
text = request.downloadHandler.text;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment