错误代码:
/// <summary>
/// 通过 Web 请求代理辅助器发送请求。
/// </summary>
/// <param name="webRequestUri">要发送的远程地址。</param>
/// <param name="userData">用户自定义数据。</param>
public override void Request(string webRequestUri, object userData)
{
if (m_WebRequestAgentHelperCompleteEventHandler == null || m_WebRequestAgentHelperErrorEventHandler == null)
{
Log.Fatal("Web request agent helper handler is invalid.");
return;
}
WWWFormInfo wwwFormInfo = (WWWFormInfo)userData;
if (wwwFormInfo.WWWForm != null)
{
m_UnityWebRequest = UnityWebRequest.Post(webRequestUri, wwwFormInfo.WWWForm);
}
else if (wwwFormInfo.JsonData != null)
{
m_UnityWebRequest = UnityWebRequest.Post(webRequestUri, "POST");
m_UnityWebRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
m_UnityWebRequest.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(wwwFormInfo.JsonData));
}
else
{
m_UnityWebRequest = UnityWebRequest.Get(webRequestUri);
}
SendWebRequest();
}
错误原因
具体原因不明,总之加了这行代码就会有问题,有大佬知道的请评论指导下,蟹蟹
m_UnityWebRequest.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(wwwFormInfo.JsonData));
解决办法
将Post方法中的第二个参数改成WWWFrom类型的
m_UnityWebRequest = UnityWebRequest.Post(webRequestUri, wwwFormInfo.WWWForm);
m_UnityWebRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
m_UnityWebRequest.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(wwwFormInfo.JsonData));
最后
因为整体代码和上面有些重复,修改了下,完整代码
/// <summary>
/// 通过 Web 请求代理辅助器发送请求。
/// </summary>
/// <param name="webRequestUri">要发送的远程地址。</param>
/// <param name="userData">用户自定义数据。</param>
public override void Request(string webRequestUri, object userData)
{
if (m_WebRequestAgentHelperCompleteEventHandler == null || m_WebRequestAgentHelperErrorEventHandler == null)
{
Log.Fatal("Web request agent helper handler is invalid.");
return;
}
WWWFormInfo wwwFormInfo = (WWWFormInfo)userData;
if (wwwFormInfo.WWWForm != null)
{
m_UnityWebRequest = UnityWebRequest.Post(webRequestUri, wwwFormInfo.WWWForm);
}
else
{
m_UnityWebRequest = UnityWebRequest.Get(webRequestUri);
}
if (wwwFormInfo.JsonData != null)
{
m_UnityWebRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
m_UnityWebRequest.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(wwwFormInfo.JsonData));
}
SendWebRequest();
}
其它
网上找到的方法时使用using定义WebRequest的作用范围,执行结束时自动释放,或者手动释放WebRequest。如下:
public static IEnumerator UnityWebRequestPost(string url, string json, Action<string> acticn)
{
using (UnityWebRequest www = UnityWebRequest.Post(url, json))
{
www.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
www.uploadHandler = new UploadHandlerRaw(bodyRaw);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ProtocolError || www.result == UnityWebRequest.Result.ConnectionError)
{
Debug.LogError($"发起网络请求失败: 确认过闸接口 -{www.error}");
}
else
{
Debug.Log(www.downloadHandler.text);
acticn?.Invoke(www.downloadHandler.text);
}
}
}
显然这个方法不适用于我们这个需求,这样要改得地方太多了。