UnityWebRequest和/或HttpWebRequest在Android上使用PUT

时间:2016-04-01 19:31:25

标签: c# android unity3d network-programming put

((我也在Unity Answers和GameDev上发布了这个,但是在这里发布x-post,以防任何C#网络负责人在我的HttpWebRequest代码中指出任何明显的错误......)

我试图通过他们的SDK将通过我的游戏捕获的图像发布到Facebook,并且因为它需要URI,我编写了一个简单的AWS Lambda函数来获取字节数组并将其上传到AWS桶。 AWS函数接受调用,然后返回一个URL以调用PUT到图像数据... 因此,如果响应为200,则使用该URL发布到FB。

但是 - 使用Unity 5.2.x到5.3.4和Android 4.4 - 6.1 - 它总是给我一个403响应,同时在iOS上工作正常。

所以,我有一个接受byte []然后执行此操作的方法:

    WWW w = new WWW("https://my-amazon-function-to-call");
    yield return w;
    if (!string.IsNullOrEmpty(w.error)) {
        Debug.LogError(w.error);
    }
    else 
    {
             var dict = Json.Deserialize(w.text) as Dictionary<string,object>;
             string oneTimeUploadUrl = (string)dict["oneTimeUploadUrl"]; // Private URL
             string resultUrl = (string)dict["resultUrl"]; // Public URL

            UnityWebRequest aws = UnityWebRequest.Put(oneTimeUploadUrl, bytes);
            yield return aws.Send();
            if(aws.isError) 
            {
                Debug.LogError("AWS ERROR: " + aws.error);
            }
            if(aws.responseCode == 200)
            {
                FeedShare(new Uri(resultUrl), _cachedMessage);    // FB call
            }
    }

非常简单,对吧? 在iOS上,是的。但是它不断给予我关于PUT操作的403响应。

所以,我已经把它包装在一个特定于iOS的#ifdef中,并尝试了一些更原生的C-Sharp-ish for Android ......例如:

    WWW w = new WWW("https://my-amazon-function-to-call"); // tried pure old Http too
    yield return w;
    if (!string.IsNullOrEmpty(w.error)) {
        Debug.LogError(w.error);
    }
    else {
        var dict = Json.Deserialize(w.text) as Dictionary<string,object>;
        string oneTimeUploadUrl = (string)dict["oneTimeUploadUrl"];
        string resultUrl = (string)dict["resultUrl"];

    #if UNITY_IPHONE
        UnityWebRequest aws = UnityWebRequest.Put(oneTimeUploadUrl, bytes);
        yield return aws.Send();
        if(aws.isError) 
        {
            Debug.LogError("AWS ERROR: " + aws.error);
        }
        if(aws.responseCode == 200)
        {
            FeedShare(new Uri(resultUrl), _cachedMessage);    
        }
    #else             

    // Various Security Callback Tests
        //ServicePointManager.ServerCertificateValidationCallback = (p1, p2, p3, p4) => true;
        //ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
        ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);


        HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(oneTimeUploadUrl);
        //wreq.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
        //wreq.PreAuthenticate = false;
        wreq.Method = "PUT";
        wreq.ContentType = "image/png";
        wreq.ContentLength = bytes.Length;

        Stream newStream = wreq.GetRequestStream();
        newStream.Write(bytes, 0, bytes.Length);
        newStream.Close();

        HttpWebResponse response = (HttpWebResponse)wreq.GetResponse();
        if((int)response.StatusCode == 200)
        {
            FeedShare(new Uri(resultUrl), _cachedMessage);
        }
     #endif
    }

...但这也给了我403.我尝试了几个不同的选项,你可以从注释掉的代码中看到,但没有爱。对于记录,以下是用作ServerCertificateValidationCallbacks的SSL策略函数:

public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
    return true;
}

public bool MyRemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
    bool isOk = true;
    if (sslPolicyErrors != SslPolicyErrors.None) {
        for (int i=0; i<chain.ChainStatus.Length; i++) {
            if (chain.ChainStatus [i].Status != X509ChainStatusFlags.RevocationStatusUnknown) {
                chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
                chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
                chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan (0, 1, 0);
                chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
                bool chainIsValid = chain.Build ((X509Certificate2)certificate);
                if (!chainIsValid) {
                    isOk = false;
                }
            }
        }
    }
    print("MyRemoteCertificateValidationCallback: " + isOk);
    return isOk;
}

这是实际的StackTrace:

04-01 11:14:57.325: I/Unity(22979):  
04-01 11:14:57.325: I/Unity(22979): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 37)
04-01 11:14:57.825: I/Unity(22979): WebException: The remote server returned an error: (403) Forbidden.
04-01 11:14:57.825: I/Unity(22979):   at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x00000] in <filename unknown>:0 
04-01 11:14:57.825: I/Unity(22979):   at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00000] in <filename unknown>:0 
04-01 11:14:57.825: I/Unity(22979):  
04-01 11:14:57.825: I/Unity(22979): (Filename:  Line: -1)

此时,我完全陷入困境......我唯一想到的可能就是写一个原生插件......但是想要 NOT 不得不这么做......

想法???

1 个答案:

答案 0 :(得分:2)

我遇到了类似的问题,我总是得到403.然后我发现我的服务器正在检查 Content-Type HEADER 中的> User-Agent 。我把它们放在它之后,它的作品。我的代码是:

UnityWebRequest www = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
byte[] body = new System.Text.UTF8Encoding().GetBytes(json_string);
www.uploadHandler = (UploadHandler)new UploadHandlerRaw(body);
www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
www.SetRequestHeader("Content-Type", "application/json");
www.SetRequestHeader("User-Agent", "DefaultBrowser");
www.SetRequestHeader("Cookie", string.Format("DummyCookie"));
www.chunkedTransfer = false;
yield return www.Send();