首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用WWWForm格式错误的HTTP

使用WWWForm格式错误的HTTP
EN

Stack Overflow用户
提问于 2014-02-18 14:34:07
回答 1查看 5.5K关注 0票数 1

我使用UnityHTTP (https://github.com/andyburke/UnityHTTP)来调用REST ( KiiCloud http://www.kii.com ),它工作得很好,但如果可能的话,我想摆脱第三方库,并使用联合的WWW和WWWForm来实现同样的功能。

下面是使用UnityHTTP的代码,运行良好:

代码语言:javascript
复制
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时:

代码语言:javascript
复制
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”:“这是回显的!”}数据:

代码语言:javascript
复制
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工作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-19 21:22:02

正如我在评论中提到的:C#将所有字符串转换为UTF-16。。如果您的not服务器需要进行不同的编码,那么简单地逐字传递字节将不会产生好的结果。

JSON通常在UTF-8中编码。,但是最好是API显式地指定它的输入/输出编码。

今天我花了更多的时间。如果您检查UnityHTTP的源代码,您可以看到他们的Hashtable构造函数在UTF-8中编码JSON

代码语言:javascript
复制
    this.bytes = Encoding.UTF8.GetBytes( JSON.JsonEncode( data ) );

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21856918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档