UnityWebRequest不发送POST数据

When Unity sends the POST request it doesn't pass the POST data, so the server returns an error (it gets the server response). I've seen that several people had a similar issue and it got fixed by adding www.chunkedTransfer = false;, however, that doesn't work for me.

I've also seen that some people use WWWForm instead of IMultipartFormSection, but I haven't tried it because it is deprecated.

I'm using PHP, but I've also tried it with Perl and it didn't work either. When I manually send a POST request everything works normally, so it seems the issue is in Unity. I'm new to Unity, so any help would be appreciated. I'm using the current latest version, 2018.2.18f1 Personal.

My code is pretty much the same as the official Unity documentation for sending POST request, but apparently it doesn't work. Here is my code:

C#:

public void Click() {
    StartCoroutine(PostRequest("http://127.0.0.1/test.php", "help"));
}

IEnumerator PostRequest(string url, string data) {
    List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
    formData.Add(new MultipartFormDataSection("data=" + data));

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

    if (www.isNetworkError || www.isHttpError) {
        Debug.Log(www.error);
    } else {
        Debug.Log(www.downloadHandler.text);
    }
}

PHP:

<?php echo "Server received: " . $_POST["data"]; ?>
dongzilu0178
dongzilu0178 哇,这实际上解决了这个问题
一年多之前 回复
dongwan5381
dongwan5381 从来没有使用统一,但根据docs.unity3d.com/ScriptReference/...我想它应该是MultipartFormDataSection(“数据”,数据)
一年多之前 回复
dongshao2967
dongshao2967 在PHP中,您可以从php://输入流中转储出来以查看请求正文的外观。
一年多之前 回复

1个回答

Christoph Lütjen pointed out that according to this it should be new MultipartFormDataSection("data", data), despite the official documentation example using new MultipartFormDataSection("field1=foo&field2=bar").

Changing it to new MultipartFormDataSection("data", data) fixed the issue.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问