Unity应用程序没有响应并获得"请求超时"调用WebAPI时

时间:2015-05-29 14:45:20

标签: api unity3d timeout

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

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

它卡在那里,团结没有响应,再次正常工作后,调试显示请求超时。

以下是我在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()

更新:问题已解决。 WebAPI返回类型为bool,但我的DLL强制返回String。

1 个答案:

答案 0 :(得分:0)

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