Unity3d 使用WWWForm的格式错误的HTTP post

Unity3d 使用WWWForm的格式错误的HTTP post,unity3d,kiicloud,Unity3d,Kiicloud,我正在使用UnityHTTP()调用restapi(KiiCloud),它工作得很好,但如果可能的话,我想摆脱第三方库,并使用Unity的WWW和WWWForm实现同样的功能 下面是使用UnityHTTP的代码,该代码运行良好: public static void RunServerExtension (string appId, string appKey, string endpoint, string kii_access_token, string msg) { Hashtab

我正在使用UnityHTTP()调用restapi(KiiCloud),它工作得很好,但如果可能的话,我想摆脱第三方库,并使用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编码/解码),因此此方法将{“message”:“this is echood!!”}作为数据传递:


但仍然是同样糟糕的要求。你明白为什么这会失败吗?UnityHTTP为什么工作?

正如我在评论中提到的:。如果您的Web服务器需要不同的编码,那么简单地逐字传递字节将不会产生好的结果

,但最好是API显式指定其输入/输出编码

今天我花了更多的时间。如果您检查UnityHTTP的源代码,您可以看到:


您的代码没有更改字符串的编码,这意味着您发送了错误的字节。

GetBytes
中发生了什么?C#,如果您的服务器希望输入其他编码(如UTF-8).Thx,则可能会导致问题。我不确定这是否是问题所在,但我已将字节[]转换的代码添加到原始Post中。感谢您花时间查看UnityHTTP的代码。这实际上就是问题所在。我知道我所做的转换的局限性,但决定在做更多的测试时忽略它。糟糕的决定。Thx人
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;
}
    this.bytes = Encoding.UTF8.GetBytes( JSON.JsonEncode( data ) );