Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

unity3d - UnityWebRequest POST to PHP not work

I use documentation with 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 );
    }
}

PHP:

<?php 

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

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

?>

Response (MULTIPART and WWWForm):

POST: Array
(
)
array(0) {
}
GET: Array
(
)
array(0) {
}

$_POST is empty... (that's my problem)

Use Unity 2017.3.

I used UnityWebRequest.Send (Unity 5.6) before and it worked for me until it became obsolete.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...