【问题标题】:UnityWebRequest POST not sending to ASP.NET APIUnityWebRequest POST 未发送到 ASP.NET API
【发布时间】:2019-02-25 19:39:39
【问题描述】:

我正在使用 UnityWebRequests 发布数据。不幸的是,这不起作用,尽管我没有收到任何错误。

我已经使用 ASP.NET Core 制作了一个 API,该 API 正在运行(使用 Postman 测试)。我尝试了多种“解决方案”:发送字节、字符串中的 JSON、请求标头、WWW,但它们似乎都不起作用。

API 控制器中的 POST 部分

    // POST: api/Todo
    [HttpPost]
    public async Task<ActionResult<LeaderboardItem>> 
    PostLeaderboardItem(LeaderboardItem item)
    {
        _context.LeaderboardItems.Add(item);
        await _context.SaveChangesAsync();

        return CreatedAtAction(nameof(GetLeaderboardItem), new { id = item.Id }, item);
    }

UnityWebRequest

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

[Serializable]
public class MiniGame
{
public int id;
public int teamId;
public int score;
}

public class JSON_Test : MonoBehaviour
{
void Start()
{
    MiniGame miniGame = new MiniGame();
    miniGame.id = 9999;
    miniGame.teamId = 10;
    miniGame.score = 10;

    StartCoroutine(PostRequest(miniGame));
}

public IEnumerator PostRequest(MiniGame miniGame)
{
    string jsonData = JsonUtility.ToJson(miniGame);
    Debug.Log(jsonData);

    using (UnityWebRequest request = 
UnityWebRequest.Get("https://localhost:44326/api/leaderboard"))
    {
        request.method = UnityWebRequest.kHttpVerbGET;
        request.SetRequestHeader("Content-Type", "application/json");
        request.SetRequestHeader("Accept", "application/json");
        yield return request.SendWebRequest();
        //if (!request.isNetworkError && request.responseCode == (int)responseCodes.OK)
        //{
        //    Debug.Log("Data succesfully sent to the server");
        //}
        //else
        //{
        //    Debug.Log("Error sending data to the server");
        //}
    }
   }
 }

有什么帮助吗?

【问题讨论】:

  • 您是否看到尝试访问网络服务器上的 URL?

标签: asp.net api unity3d unitywebrequest


【解决方案1】:

我在从 asp core web api 获取请求时遇到了一些类似的问题。我的解决方案是使用 http 而不是 https 作为 web api

【讨论】:

    【解决方案2】:

    UnityWebRequest.Get("https://localhost:44326/api/leaderboard"))

    尝试使用您的本地 IP 地址,而不是 localhost。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 2016-05-12
      • 2020-04-12
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多