untiyWebRequest发送post请求,请求参数为json

private IEnumerator SendHttpRequest(string jsonStr, Action<string> callBack)//一个通用的请求
    {
        string url = webModel.serverAddress + webModel.tokenAddress;

        UnityWebRequest request = UnityWebRequest.Post(url, "POST");
        request.SetRequestHeader("Content-Type", "application/json");//添加请求头,指定body中的内容为json
        request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(jsonStr));//添加raw类型的body,内容为json的byte[]
        yield return request.SendWebRequest();
        if (request.isHttpError || request.isNetworkError)
        {
            Debug.LogError(request.error);
        }
        else
        {
            //如果没有错,处理服务器返回的信息;
            string code = request.downloadHandler.text;
            callBack?.Invoke(code);//调用回调
        }
    }

你可能感兴趣的:(untiyhttp,unity,unity)