我正在使用 UnityWebRequests 发布数据。不幸的是,这不起作用,尽管我没有收到任何错误。
我用 ASP.NET Core 制作了一个 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");
//}
}
}
}
有什么帮助吗?