【发布时间】:2016-01-05 22:20:47
【问题描述】:
我在网上找不到任何线索,因为我想很多人已经放弃了 Unity 的 WWW 类来寻找复杂的东西。我正在使用 WWW 类与我的 REST API 对话,并且我需要找到一种方法来在每个请求之后执行一些代码。 (如果响应为 401,我需要检查响应代码并执行一些默认行为)
有没有简单的方法来实现这一点?
(我正在使用协程发送请求)
提前致谢。
更新:当前代码示例
Auth.cs:
public IEnumerator Login(WWWForm data, Action<APIResponse> success, Action<APIResponse> failure)
{
WWW request = new WWW (API + "auth/authenticate", data);
yield return request;
if (request.responseHeaders ["STATUS"] == "HTTP/1.1 401 Unauthorized") {
//Do something, I want to do this on every request, not just on this login method
}
if (request.error != null) {
failure (new APIResponse(request));
}
else {
//Token = request.text.Replace("\"", "");
Token = request.text;
Debug.Log (Token);
success (new APIResponse (request));
}
}
用法:
StartCoroutine (auth.Login (data, new Action<APIResponse> (response => {
//Do stuff
}), new Action<APIResponse> (response => {
//Do stuff
})));
【问题讨论】:
-
更新了我的帖子以包含一些代码。
标签: c# web unity3d webrequest