Unity 应用无响应,调用 WebAPI 时"request time out"



我有一个Unity应用程序调用DLL中的Like()方法,它连接到WebAPI来完成这项工作。但是,当我调试应用程序和代码运行到

string liked = SocialConnector.LikePost(token, postID);

它卡在那里,unity变得没有响应,工作恢复正常后,调试显示请求超时。

下面是我在DLL中的详细代码

public static String LikePost(String token, String postID)
    {
        HttpCommon common = new HttpCommon();
        string url = Constant.ApiURL + Constant.API_LINK_POST_LIKE_UNLIKE + "?postID=" + postID;
        String result = common.HttpPost<String>(url, token);
        return result;
    }
HTTPCommon 中的代码
public T HttpPost<T>(String url, String token)
    {
        var request = (HttpWebRequest)WebRequest.Create(url);
        //var data = Encoding.ASCII.GetBytes(postData);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        //request.ContentLength = data.Length;
        request.Headers["Authorization"] = "Bearer " + token;
        var stream = request.GetRequestStream();
        var response = (HttpWebResponse)request.GetResponse();
        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
        try
        {                
            var responseData = JsonConvert.DeserializeObject<T>(responseString);
            return responseData;
        }
        catch (Exception e)
        {
            throw new Exception(responseString);
        }
    }

错误日志:

WebException: The request timed out
System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult)
System.Net.HttpWebRequest.GetResponse ()
GSEP_DLL.Connectors.HttpCommon.HttpPost[String] (System.String url,      System.String token)
GSEP_DLL.Connectors.SocialConnector.LikePost (System.String token,   System.String postID)
Like.onClick () (at Assets/Scripts/Like.cs:39)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at    C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:110)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at    C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:574)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:716)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick    (UnityEngine.EventSystems.PointerEventData eventData) (at  C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler     handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler]   (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()

Update: Problem solved。WebAPI返回bool类型,但我的DLL强制返回String.

问题解决。WebAPI返回bool类型,但我的DLL强制返回String.

相关内容

  • 没有找到相关文章

最新更新