繁体   English   中英

"Unity Web Request Post 不发送数据"

[英]Unity Web Request Post not sending data

统一2019.2.1f1

我已经尽可能多地查看了其他解决方案,但似乎没有一个能解决我的问题。

当使用 UnityWebRequest 或 WWW 将 WWWForm 发送到 php 时,表单数据永远不会被读取。

这是我使用 UnityWebRequest 的 c# 代码:

    WWWForm formData = new WWWForm ();
    formData.AddField ("firstname", "firstname");

    UnityWebRequest www = UnityWebRequest.Post (URL, formData);
    //www.chunkedTransfer = false; //<- Tested with this on and off and no difference
    yield return www.SendWebRequest ();

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

试试这个代码,并阅读关于UnityWebRequest统一文件

private void Start()
{
    StartCoroutine(GetRequest(url));
}

private IEnumerator GetRequest(string uri)
{
    var formData = new WWWForm();
    formData.AddField("firstname", "firstname");

    using (var webRequest = UnityWebRequest.Post(uri, formData))
    {
        yield return webRequest.SendWebRequest();

        string[] pages = uri.Split('/');
        int page = pages.Length - 1;

        if (webRequest.isNetworkError)
        {
            Debug.Log(pages[page] + ": Error: " + webRequest.error);
        }
        else
        {
            Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
        }
    }
}

结果

感谢所有在这方面帮助我的人,您的所有答案都是正确的。 URL必须是HTTPS而不是HTTP。 我的错 :/

我复制了您的代码并执行了以下操作:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class pp : MonoBehaviour
{

    bool runReuqest = true;
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if (runReuqest)
            StartCoroutine("runRquest");
    }

    IEnumerator runRquest() {
        runReuqest = false;
        // Your Code no changes
        WWWForm formData = new WWWForm();
        formData.AddField("firstname", "firstname");

        UnityWebRequest www = UnityWebRequest.Post("http://my-ip/j.php", formData);
        //www.chunkedTransfer = false; //<- Tested with this on and off and no difference
        yield return www.SendWebRequest();

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

我得到的日志如下:

Log:
Form upload complete!
UnityEngine.Debug:Log(Object)

HELLOfirstname!!!!
UnityEngine.Debug:Log(Object)

检查您的代码是否在某处搞砸了,或者使用上面的作为指导。

编辑 1我没有去检查以前的答案,但它似乎对我也有用。 j.php: 收到: HELLOfirstname!!!!

您可能还缺少其他东西。

我有或有同样的问题,所以我使用以下方法:这里的想法是发送一个名为 POST 的字段,然后让 php 脚本检查 POST,使用 isset($_POST["POST"])

WWWForm form = new WWWForm();
form.AddField("post","POST");  // Added line to check for post
form.AddField("userID", userID);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM