Edited at

UnityでTHETA Vを操作するときの注意点(UnityWebRequest)


はじめに


  • UnityでTHETA V操作アプリを作成しようとしたところ変なところで引っかかったのでメモ


現象


  • UnityWebRequestでTHETA V(v2.41.1)を操作しようとしたところ以下の問題が起きた


    • GETは成功する

    • POSTがAndroid(Galaxy8、OculusGo)で失敗する(Macでは動作)


      • 失敗時は、Request Time Outになる






原因


  • UnityWebRequestのPOST実行時、Android版はヘッダに「Expect: 100-continue」が付くため


対策


  • 「Expect: 100-continue」を外す


    • UnityWebRequest.useHttpContinue = false を設定




実際のコード


ThetaAccessControl.cs

using System.Collections;

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
using System;
using System.Text;
using System.IO;

public class ThetaAccessControl {

public string baseUrl = "http://192.168.1.1";

public int requestTimeout = 5;

void Start() {
}

public IEnumerator Get(string url, string key, UnityAction<string, string, byte[]> action, UnityAction<string, UnityWebRequest> error) {
var request = UnityWebRequest.Get(url);
request.timeout = requestTimeout;

// リクエスト送信
yield return request.SendWebRequest();

// 通信エラーチェック
if (request.isNetworkError) {
if (error != null) {
error(key, request);
}
} else {
if (request.responseCode == 200) {
if(action != null) {
byte[] results = request.downloadHandler.data;
action(key, url, results);
}
} else {
if (error != null) {
error(key, request);
}
}
}
}
public IEnumerator Post(string url, string bodyJsonString, string key, UnityAction<string, string, byte[]> action, UnityAction<string, UnityWebRequest> error) {
var request = new UnityWebRequest(url, "POST");
if (bodyJsonString != "") {
byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
}
request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.timeout = requestTimeout;
request.useHttpContinue = false;

yield return request.SendWebRequest();

if (request.isNetworkError) {
if (error != null) {
error(key, request);
}
} else {
if (action != null) {
string result = request.downloadHandler.text;
byte[] resultByte = request.downloadHandler.data;
action(key, result, resultByte);
}
}
}
}



参考


備考


  • これって、、、