Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTTP请求在一段时间后停止工作_Http_Mono_Webclient - Fatal编程技术网

HTTP请求在一段时间后停止工作

HTTP请求在一段时间后停止工作,http,mono,webclient,Http,Mono,Webclient,我用Unity(Mono)编写的游戏有奇怪的问题。我有登录功能,成功登录后,我会发送一些保持活动状态的请求,以确保令牌更新(我每30秒发送一次) 问题是,经过一段时间后(有时是1小时,有时是2.5小时),我的所有请求都处于超时状态 为了确定我的连接状态,我在代码中做了一些检查:我正在简单地访问我的API的主网站(不是API调用,只是访问网站)。当我下次在API上超时时,我发现: 超时1次后,我总是这样。重新启动应用程序有助于 我在API调用和获取基本API网站的请求时超时 谷歌仍在回复状态为2

我用Unity(Mono)编写的游戏有奇怪的问题。我有登录功能,成功登录后,我会发送一些保持活动状态的请求,以确保令牌更新(我每30秒发送一次)

问题是,经过一段时间后(有时是1小时,有时是2.5小时),我的所有请求都处于超时状态

为了确定我的连接状态,我在代码中做了一些检查:我正在简单地访问我的API的主网站(不是API调用,只是访问网站)。当我下次在API上超时时,我发现:

  • 超时1次后,我总是这样。重新启动应用程序有助于
  • 我在API调用和获取基本API网站的请求时超时
  • 谷歌仍在回复状态为200(此处无超时)
实施: 一开始我一直在使用
RestSharp
来处理请求,但问题发生了,于是决定扔掉
RestSharp
,现在我们使用的是经典
WebClient

class BetterWebClient : WebClient
{
    private WebRequest _Request = null;
    public TimeSpan? Timeout { get; set; }
    protected override WebRequest GetWebRequest(Uri address)
    {
        this._Request = base.GetWebRequest(address);
        if ( Timeout.HasValue )
        {
            _Request.Timeout = (int)Timeout.Value.TotalMilliseconds;
        }
        else
        {
            _Request.Timeout = 10*1000; //10s
        }
        if (this._Request is HttpWebRequest)
        {
            ((HttpWebRequest)this._Request).AllowAutoRedirect = true;
        }

        return this._Request;
    }
}
我的HandlerRequest函数(也称为Google和API网站)如下所示:

public static void HandleRequest<TR>(string url, RestResponse<TR> executeGetRequest) where TR : new()
{
    using (BetterWebClient w = new BetterWebClient() {Timeout = TimeSpan.FromSeconds(2)})
    {
        try
        {
            string downloadString = w.DownloadString("http://www.google.com");
            Debug.Log("Google request: OK");
        }
        catch ( Exception )
        {
            Debug.LogError("Google request failed");
        }

        try
        {
            string downloadString = w.DownloadString("myAPIwebsite");
            Debug.Log("WorldOfRescue.com request: OK");
        }
        catch ( Exception )
        {
            Debug.LogError("WorldOfRescue.com request failed");
        }
    }
    var client = new BetterWebClient() {Timeout = TimeSpan.FromSeconds(5)};
    client.Headers.Add("Accept", "application/json");
    client.Headers.Add("Content-Type", "application/json");
    try
    {
        string downloadString = client.DownloadString(url);

        Debug.Log("Request for "+url+ " completed. Response: " + downloadString);
    }
    catch ( WebException e )
    {
        Debug.Log(e.Status);

        throw;
    }
    catch ( Exception e )
    {
        Debug.Log(e.ToString());
        throw;
    }
    finally
    {
        client.Dispose();
    }
}
publicstaticvoidhandlerequest(字符串url,resresponse executeGetRequest),其中TR:new()
{
使用(BetterWebClient w=new BetterWebClient(){Timeout=TimeSpan.FromSeconds(2)})
{
尝试
{
string downloadString=w.downloadString(“http://www.google.com");
Log(“谷歌请求:OK”);
}
捕获(例外)
{
LogError(“谷歌请求失败”);
}
尝试
{
string downloadString=w.downloadString(“myAPIwebsite”);
Log(“WorldOfRescue.com请求:OK”);
}
捕获(例外)
{
LogError(“WorldOfRescue.com请求失败”);
}
}
var client=new BetterWebClient(){Timeout=TimeSpan.FromSeconds(5)};
client.Headers.Add(“Accept”、“application/json”);
client.Headers.Add(“内容类型”、“应用程序/json”);
尝试
{
string downloadString=client.downloadString(url);
Log(“请求“+url+”已完成。响应:“+downloadString”);
}
捕获(WebE例外)
{
调试日志(如状态);
投掷;
}
捕获(例外e)
{
Log(例如ToString());
投掷;
}
最后
{
client.Dispose();
}
}
你知道为什么会这样吗?看起来好像有什么东西阻止了我向特定网站发送请求,但同时其他网站工作正常