UnityWebRequest POST到PHP不起作用

Ser*_*gey 3 php post webrequest unity-game-engine

我使用WWWForm的文档:https://docs.unity3d.com/Manual/UnityWebRequest-SendingForm.html

C#:

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

IEnumerator Upload(){
    yield return Upload1();
    yield return Upload2();
}

IEnumerator Upload1() {
    List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
    formData.Add( new MultipartFormDataSection("field1=foo&field2=bar") );
    //formData.Add( new MultipartFormFileSection("my file data", "myfile.txt") );

    UnityWebRequest www = UnityWebRequest.Post(url, formData);
    yield return www.SendWebRequest();

    if(www.isNetworkError || www.isHttpError) {
        Debug.Log(www.error);
    }
    else {
        Debug.Log("Form upload complete!");
        Debug.Log( "MULTIPART: " + www.downloadHandler.text );
    }
}

IEnumerator Upload2() {
    WWWForm form = new WWWForm();
    form.AddField("myField", "myData");

    UnityWebRequest www = UnityWebRequest.Post(url, form);
    yield return www.SendWebRequest();

    if(www.isNetworkError || www.isHttpError) {
        Debug.Log(www.error);
    }
    else {
        Debug.Log("Form upload complete!");
        Debug.Log( "WWWForm: " + www.downloadHandler.text );
    }
}
Run Code Online (Sandbox Code Playgroud)

PHP:

<?php 

    echo "POST: ";
    print_r( $_POST );
    var_dump( $_POST );

    echo "GET: ";
    print_r( $_GET );
    var_dump( $_GET );

?>
Run Code Online (Sandbox Code Playgroud)

响应(MULTIPART和WWWForm):

POST: Array
(
)
array(0) {
}
GET: Array
(
)
array(0) {
}
Run Code Online (Sandbox Code Playgroud)

$ _POST是空的......(这是我的问题)

使用Unity 2017.3.

之前我使用过UnityWebRequest.Send(Unity 5.6),它对我起作用,直到它变得过时.

谢谢.

小智 6

这似乎对我有用,我希望它有所帮助.

UnityWebRequest www = UnityWebRequest.Post(url, formData);

www.chunkedTransfer = false;////ADD THIS LINE

yield return www.SendWebRequest();
Run Code Online (Sandbox Code Playgroud)