我正在使用UnityHTTP(https://github.com/andyburke/UnityHTTP)来调用REST API(KiiCloud http://www.kii.com)并且它工作得很好但我想尽可能摆脱第三方库并使用Unity的WWW和WWWForm实现了同样的目标。
这是使用UnityHTTP工作正常的代码:
public static void RunServerExtension (string appId, string appKey, string endpoint, string kii_access_token, string msg)
{
Hashtable data = new Hashtable();
// Add json fields with values here (use as dictionary)
data.Add("message", msg);
// When you pass a Hashtable as the third argument, we assume you want it send as JSON-encoded
// data. We'll encode it to JSON for you and set the Content-Type header to application/json
HTTP.Request myRequest = new HTTP.Request( "post", "https://api.kii.com/api/apps/" + appId + "/server-code/versions/current/" + endpoint, data);
myRequest.AddHeader("x-kii-appid", appId);
myRequest.AddHeader("x-kii-appkey", appKey);
if(kii_access_token != null)
theRequest.AddHeader("Authorization", "Bearer " + kii_access_token);
myRequest.Send( ( request ) => {
// we provide Object and Array convenience methods that attempt to parse the response as JSON
// if the response cannot be parsed, we will return null
// note that if you want to send json that isn't either an object ({...}) or an array ([...])
// that you should use JSON.JsonDecode directly on the response.Text, Object and Array are
// only provided for convenience
Hashtable result = request.response.Object;
if ( result == null )
{
Debug.LogWarning( "Could not parse JSON response!" );
return;
}
Debug.Log ("Got response");
Debug.Log(request.response.Text);
});
}
所以上面的工作正常,但是当我以这种方式切换到WWWForm时:
public static WWW RunServerExtension (string appId, string appKey, string endpoint, string kii_access_token, string msg)
{
WWWForm form = new WWWForm();
Hashtable headers = form.headers;
headers["Content-Type"] = "application/json";
headers["x-kii-appid"] = appId;
headers["x-kii-appkey"] = appKey;
if(kii_access_token != null)
headers["Authorization"] = "Bearer " + kii_access_token;
form.AddField("message", msg);
return new WWW("https://api.kii.com/api/apps/" + appId + "/server-code/versions/current/" + endpoint, form.data, headers);
}
private IEnumerator WaitForRequest(WWW www)
{
yield return www;
// check for errors
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.text);
} else {
Debug.Log("WWW Error: "+ www.error);
}
}
我在服务器端得到了一个不好的请求(意味着请求格式错误,而不是服务器所期望的)。请注意,标头必须作为参数传递,否则服务器会抱怨缺少标头。
我怀疑这可能与服务器需要JSON数据这一事实有关,因此我使用UnityHTTP JSON类将消息转换为JSON(您可以使用该隔离类进行JSON编码/解码)https://github.com/andyburke/UnityHTTP/blob/master/external/JSON.cs所以这方法传递 {“message”:“这是回声!!”} 作为数据:
public static WWW RunServerExtension (string appId, string appKey, string endpoint, string kii_access_token, string msg)
{
WWWForm form = new WWWForm();
Hashtable headers = form.headers;
headers["Content-Type"] = "application/json";
headers["x-kii-appid"] = appId;
headers["x-kii-appkey"] = appKey;
if(kii_access_token != null)
headers["Authorization"] = "Bearer " + kii_access_token;
Hashtable data = new Hashtable();
data["message"] = msg;
byte[] bytes = GetBytes(JSON.JsonEncode(data));
return new WWW("https://api.kii.com/api/apps/" + appId + "/server-code/versions/current/" + endpoint, bytes, headers);
}
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
但仍然是相同的不良请求。你明白为什么会失败吗?为什么UnityHTTP有效?
答案 0 :(得分:1)
正如我在评论中提到的那样:C# converts all strings to UTF-16。如果您的网络服务器期望不同的编码,只需逐字传递字节就不会产生良好的结果。
JSON is typically encoded in UTF-8,但最好是API明确指定其输入/输出编码。
今天我花了一点时间。如果您查看UnityHTTP的来源,则可以看到their Hashtable constructor encodes JSON in UTF-8:
this.bytes = Encoding.UTF8.GetBytes( JSON.JsonEncode( data ) );
您的代码不会更改字符串的编码,这意味着您发送了错误的字节。