duanluangua8850 2018-12-01 18:08
浏览 805
已采纳

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"]; ?>
  • 写回答

1条回答 默认 最新

  • douji6940 2018-12-01 18:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝