Unity(C#)から、サーバへPOSTリクエストを送信したいです。
その際、パラメータとしてx, yの2つの値をそれぞれStringで送信したいです。
以下のようなコードを書きましたが、エラーが出たため、どこが間違っているのか教えてください。m(_ _)mC#
ここに言語を入力
コード
using
1 2using System.Collections.Generic; 3 4using System.Linq; 5 6using UnityEngine; 7using System.IO; 8using System.Net; 9using System.Text; 10 11 12 13public class Example : MonoBehaviour 14 15{ 16 17 private static readonly Joycon.Button[] m_buttons = 18 19 Enum.GetValues(typeof(Joycon.Button)) as Joycon.Button[]; 20 21 22 23 private List<Joycon> m_joycons; 24 25 private Joycon m_joyconL; 26 27 private Joycon.Button? m_pressedButtonL; 28 29 30 private void Start() 31 32 { 33 34 m_joycons = JoyconManager.Instance.j; 35 36 37 38 if (m_joycons == null || m_joycons.Count <= 0) return; 39 40 41 42 m_joyconL = m_joycons.Find(c => c.isLeft); 43 44 } 45 46 47 48 private void Update() 49 50 { 51 52 m_pressedButtonL = null; 53 54 55 56 if (m_joycons == null || m_joycons.Count <= 0) return; 57 58 59 60 foreach (var button in m_buttons) 61 62 { 63 64 if (m_joyconL.GetButton(button)) 65 66 { 67 68 m_pressedButtonL = button; 69 70 } 71 72 } 73 74 75 76 if (Input.GetKeyDown(KeyCode.Z)) 77 78 { 79 80 m_joyconL.SetRumble(160, 320, 0.6f, 200); 81 82 } 83 } 84 85 86 87 private void OnGUI() 88 89 { 90 91 var style = GUI.skin.GetStyle("label"); 92 93 style.fontSize = 24; 94 95 96 97 if (m_joycons == null || m_joycons.Count <= 0) 98 99 { 100 101 GUILayout.Label("Joy-Con が接続されていません"); 102 103 return; 104 105 } 106 107 108 109 if (!m_joycons.Any(c => c.isLeft)) 110 111 { 112 113 GUILayout.Label("Joy-Con (L) が接続されていません"); 114 115 return; 116 117 } 118 119 120 121 122 123 GUILayout.BeginHorizontal(GUILayout.Width(960)); 124 125 126 127 foreach (var joycon in m_joycons) 128 129 { 130 131 var isLeft = joycon.isLeft; 132 133 var name = isLeft ? "Joy-Con (L)" : "Joy-Con (R)"; 134 135 var key = isLeft ? "Z キー" : "X キー"; 136 137 var button = isLeft ? m_pressedButtonL:m_pressedButtonL; 138 139 var stick = joycon.GetStick(); 140 141 var gyro = joycon.GetGyro(); 142 143 var accel = joycon.GetAccel(); 144 145 var orientation = joycon.GetVector(); 146 147 148 149 GUILayout.BeginVertical(GUILayout.Width(480)); 150 151 GUILayout.Label(name); 152 153 GUILayout.Label(key + ":振動"); 154 155 GUILayout.Label("押されているボタン:" + button); 156 157 GUILayout.Label(string.Format("スティック:({0}, {1})", stick[0], stick[1])); 158 159 GUILayout.Label("ジャイロ:" + gyro); 160 161 GUILayout.Label("加速度:" + accel); 162 163 GUILayout.Label("傾き:" + orientation); 164 165 GUILayout.EndVertical(); 166 167 168 // POSTする対象のURL 169 string url = "http://192.168.2.103:3000"; 170 // POSTメソッドで渡すパラメータ 171 string param = ""; 172 173 // Dictionaryオブジェクト 174 var dic = new Dictionary<string, string>(); 175 dic["x"] = "x軸方向に傾けたい角度"; 176 dic["y"] = "y軸方向に傾けたい角度"; 177 178 // POSTメソッドのパラメータ作成 179 foreach (string key in dic.Keys) 180 param += String.Format("{0}={1}&", key, dic[key]); 181 182 // paramをASCII文字列にエンコードする 183 byte[] data = Encoding.ASCII.GetBytes(param); 184 185 // リクエスト作成 186 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 187 request.Method = "POST"; 188 request.ContentType = "application/x-www-form-urlencoded"; 189 request.ContentLength = data.Length; 190 191 // ポストデータをリクエストに書き込む 192 using (Stream reqStream = request.GetRequestStream()) 193 reqStream.Write(data, 0, data.Length); 194 195 // レスポンスの取得 196 WebResponse response = request.GetResponse(); 197 198 // 結果の読み込み 199 string htmlString = ""; 200 using (Stream resStream = response.GetResponseStream()) 201 using(var reader = new StreamReader(resStream, Encoding.GetEncoding("Shift_JIS"))) 202 htmlString = reader.ReadToEnd(); 203 204 // 結果の出力 205 Console.WriteLine(htmlString); 206 207 Console.Write("Press enter to end:"); 208 Console.ReadLine(); 209 210 211 } 212 213 214 215 GUILayout.EndHorizontal(); 216 217 } 218 219} 220コード
エラーが出たのであれば、そのエラーをコピペしてください。
あなたの回答
tips
プレビュー