Hi! I am trying to wirelessly control theta z1 in my Unity app. At the very beginning, I tried the HttpWebRequest
class in c# and the script looks like this:
public class ThetaWifiStreaming : MonoBehaviour { public static string ToString(string cmd) { var sb = new StringBuilder(); sb.Append("{"); sb.Append("\"name\": \""); sb.Append(cmd); sb.Append("\", \"parameters\": {"); sb.Append("}}"); return sb.ToString(); } static void start() { var command = ToString("camera.takePicture"); var postBytes = Encoding.Default.GetBytes(command); string url = "http://192.168.12.21:80/osc/commands/execute"; string username = "THETAYN10106522"; string password = "10106522"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Credentials = new NetworkCredential(username, password); Console.WriteLine(new NetworkCredential(username, password)); request.Method = "POST"; request.Timeout = (int)(5 * 1000f); request.ContentType = "application/json;charset=utf-8"; request.ContentLength = postBytes.Length; Stream reqStream = request.GetRequestStream(); reqStream.Write(postBytes, 0, postBytes.Length); reqStream.Close(); Console.WriteLine("post done"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine((int)response.StatusCode); Console.WriteLine("response done"); } }
This works well if I copy and paste the main body of this script and run it as a new ordinary c# console app. But in unity, it will be stuck at the post done sentence and cannot get the response.
I was thinking this as an issue of HttpWebRequest
and switched to the UnityWebrequest
class. The script looks like this:
public class ThetaWifiStreaming : MonoBehaviour {
void Start() { StartCoroutine(post()); } IEnumerator post() { /* info */ var command = ToString("camera.takePicture"); var postBytes = Encoding.Default.GetBytes(command); string url = "http://192.168.12.21:80/osc/commands/execute"; string username = "THETAYN10106522"; string password = "10106522"; var authorization = authenticate(username, password); /* post */ var request = new UnityWebRequest(url, "POST"); byte[] bodyRaw = Encoding.UTF8.GetBytes(command.ToString()); request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json;charset=utf-8"); request.SetRequestHeader("Authorization", authorization); yield return request.SendWebRequest(); Debug.Log("Status Code: " + request.responseCode); } string ToString(string cmd) { var sb = new StringBuilder(); sb.Append("{"); sb.Append("\"name\": \""); sb.Append(cmd); sb.Append("\", \"parameters\": {"); sb.Append("}}"); return sb.ToString(); } string authenticate(string username, string password) { byte[] credentialBuffer = new UTF8Encoding().GetBytes(username + ":" + password); var auth = "Basic " + System.Convert.ToBase64String(credentialBuffer); return auth; } }
This script however always returns statuscode 401.
Would anybody gives me some suggestions on how to do it? Thanks!