如何检测HTTPRequest失败

时间:2015-09-29 15:03:02

标签: c# asp.net http

我发送了100000个请求,为了检查是否所有请求都已成功发送,我开发了一个简单的网页,用于计算已发送的请求数。

问题在于接收器计数小于50000并且我也无法检测到哪一个已经失败以便再次发送它们,因为发送者获得statusscode = OK所有这些并且也没有检测到异常

我删除了webReq.Method =" HEAD"但没有效果。

任何提示都表示赞赏。

以下是发件人的代码:

try
{
    var content = new MemoryStream();
    var webReq = (HttpWebRequest)WebRequest.Create(url);
    webReq.Method = "HEAD";
    using (WebResponse response = await webReq.GetResponseAsync())
    {
        HttpWebResponse res = (HttpWebResponse)response;
        if (res.StatusCode != HttpStatusCode.OK)
        {
            UnsuccessfulURLsPhase1.Add(url);
        }
    }
}
catch (Exception e)
{
    UnsuccessfulURLsPhase1.Add(url);
}

这是接收者的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                counter1++;
                txtCounter.Text = counter1.ToString();
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine("\nException raised!");
            Debug.WriteLine("Source :{0} ", ex.Source);
            Debug.WriteLine("Message :{0} ", ex.Message);
        }
    }

2 个答案:

答案 0 :(得分:0)

您的Page_Load正在吞下例外,因此服务器总是返回200,确定。

您必须抛出异常,或者必须在发生错误时显式设置响应状态。 在这里找到How to send a Status Code 500 in ASP.Net and still write to the response? 应该设置TrySkipIisCustomErrors

这样的东西
protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (!IsPostBack)
        {
            counter1++;
            txtCounter.Text = counter1.ToString();
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine("\nException raised!");
        Debug.WriteLine("Source :{0} ", ex.Source);
        Debug.WriteLine("Message :{0} ", ex.Message);

       // Raise the exception
       // throw;   
       // or assign the correct status and status code
       Response.Clear();     
       Response.TrySkipIisCustomErrors = true
       Response.ContentType = "text/plain";
       Response.StatusCode = (int)HttpStatusCode.InternalServerError;
       Response.Write(ex.Message);

       // Send the output to the client.
       Response.Flush();
    }
} 

(希望它有所帮助,自从我在WebForms中做了一些事情以来已经很长时间了:S)

答案 1 :(得分:0)

您是否尽快将这些邮件发送出去? IANA建议(以及最近版本的Windows方面)使用范围49152到65535用于临时端口,这些端口是保留用于接收来自IP套接字连接的回复的端口。这意味着有16383个端口可用,在连接关闭120秒后,每个端口必须在(IIRC)状态下保持TIME_WAIT状态。

在完美的条件下(以及可以承受数千个同时连接的路由设备......廉价的SOHO路由器可能会因内存不足而过热并变得不可靠),您将被限制为每两分钟最多约16000个请求。

实际上,HttpWebRequest(以及WebClient)将仅维护特定主机的特定数量的连接,以及通过这些连接的管道请求,因此无需调整ServicePointManager或与您尝试的主机关联的ServicePoint点击,你将有大量的排队通过这些连接挤压100000请求。您可能会在此路径的某个地方遇到超时。

相关问题