我正在尝试使用其其余 API 将 bKash Payment Gateway (PGW) 集成到 Unity 中。第一步是生成令牌。为此,它需要在其
/token/grant
api 中传递用户名和密码。问题出在密码上。因为它包含特殊字符,所以 api 失败并出现错误。除了请求标头之外,没有选项可以更改密码或将密码传递到其他任何地方。有什么办法解决这个问题吗?
它在邮递员中工作得非常好。如果密码不包含“)”,我的代码也可以正常工作
我的代码:
private IEnumerator CallApi(string api, string json, Action<string> onResponse) {
var request = new UnityWebRequest(api, "POST");
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Accept", "application/json");
request.SetRequestHeader("username", _bkashData.username ?? "");
request.SetRequestHeader("password", _bkashData.password ?? "");
request.SetRequestHeader("X-App-Key", _bkashData.appkey ?? "");
request.SetRequestHeader("Authorization", _token ?? "");
AppUtils.PrintLog($"bKash Api Request: {api} | Request: {json} | pass: {_bkashData.password}", AppConstants.Color.Other3);
yield return request.SendWebRequest();
AppUtils.PrintLog($"bKash Api Response: {api} | Response: {request.downloadHandler.text}", AppConstants.Color.Other3);
if (request.result == UnityWebRequest.Result.Success) {
onResponse(request.downloadHandler.text);
} else {
AppUtils.PrintLog($"bKash Api Failed: {request.error} | API: {api} | Request: {json}");
}
}
我收到的错误:
InvalidOperationException: Header value contains invalid characters
UnityEngine.Networking.UnityWebRequest.SetRequestHeader (System.String name, System.String value) (at <46c9a6f2bcd549f2b7ce644973f43ea8>:0)
bKashHandler+<CallApi>d__15.MoveNext () (at Assets/Scripts/Utils/bKashHandler.cs:71)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <e509afeff7384f24a8f0ac30527ff01c>:0)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
bKashHandler:GetToken(Action) (at Assets/Scripts/Utils/bKashHandler.cs:55)
bKashHandler:CreatePayment(PriceData) (at Assets/Scripts/Utils/bKashHandler.cs:112)
DepositFundScript:OnSubmit() (at Assets/Scripts/GUI/DepositFundScript.cs:328)
UnityEngine.EventSystems.EventSystem:Update() (at ./Library/PackageCache/[email protected]/Runtime/UGUI/EventSystem/EventSystem.cs:530)
测试密码以重现问题:
")^[su0ZC27M"
对我来说,RFC7230 第 3.2.6 节 对于如何正确编码标头值并不是非常清楚。似乎带引号的字符串是可能/允许的,唯一需要反斜杠编码的值是双引号和反斜杠字符本身。
Unity 的 API 不太清楚它可能对值应用哪种编码或处理方式,因此不清楚是否需要您自己处理。尽管只是双引号该值(并确保
"
和 \
被解释为 \"
和 \\
)。对我来说,Unity 的 WebRequest 应该自行处理这种转义,因此它可能是UnityWebRequest
实现的缺点
也许我错了,我在如此低的级别上处理 HTTP 已经太久了:)