UnityWebRequest POST to PHP not work

后端 未结 4 714
孤独总比滥情好
孤独总比滥情好 2020-12-11 22:19

I use documentation with WWWForm: https://docs.unity3d.com/Manual/UnityWebRequest-SendingForm.html

C#:

void Start() {
    StartCoroutine(Upload());
}         


        
相关标签:
4条回答
  • 2020-12-11 22:46

    Right now I'm trying the same thing and since WWW class and WWWForm will be deprecated in the next version I tried to found a solution to this problem that does not use the deprecated class. In the end I understand that you don't actually need to use List if you just need to send a form with strings (example, right now I'm using it for a http post for code-flow auth in dropbox). Just use a Dictionary and you'll be fine (you will not even need to use www.chunkedTransfer = false;).

    Dictionary<string, string> form = new Dictionary<string, string>();
    form.Add("code", token);
    form.Add("grant_type", "authorization_code");
    form.Add("client_id", myid);
    form.Add("client_secret", mysecret);
    form.Add("redirect_uri", "http://" + localhost + ":" + port + '/');
    
    0 讨论(0)
  • 2020-12-11 22:55

    I tried all of the above answers and nothing worked, but I noticedd that Unity adds a Request Header :

    upgrade-insecure-requests  1
    

    So I upgraded my url to https://

    It still didn't work with MultipartFormData, but it started working with WWWForm form = new WWWForm();

    0 讨论(0)
  • 2020-12-11 22:59

    Use WWWForm instead. PHP is expecting application/x-www-form-urlencoded form data, not multipart.

    WWWForm form = new WWWForm();
    form.AddField("myField", "myData");
    
    UnityWebRequest www = UnityWebRequest.Post(url, form);
    
    0 讨论(0)
  • 2020-12-11 23:08

    This seems to work for me, I hope it helps.

    UnityWebRequest www = UnityWebRequest.Post(url, formData);
    
    www.chunkedTransfer = false;////ADD THIS LINE
    
    yield return www.SendWebRequest();
    
    0 讨论(0)
提交回复
热议问题