Created
May 16, 2019 13:51
-
-
Save kurokuru/3501ab8cc14338d09304bf826fc8628e to your computer and use it in GitHub Desktop.
GAEにUnityWebRequestでgzipする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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