HttpWebResonse hangs on multiple request

Posted by Ehsan on Stack Overflow See other posts from Stack Overflow or by Ehsan
Published on 2010-05-15T04:13:33Z Indexed on 2010/05/15 4:24 UTC
Read the original article Hit count: 277

Filed under:
|
|

I've an application that create many web request to donwload the news pages of a web site (i've tested for many web sites) after a while I find out that the application slows down in fetching the html source then I found out that HttpWebResonse fails getting the response. I post only the function that do this job.

    public PageFetchResult Fetch()
    {
        PageFetchResult fetchResult = new PageFetchResult();
        try
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(URLAddress);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Uri requestedURI = new Uri(URLAddress);
            Uri responseURI = resp.ResponseUri;
            if (Uri.Equals(requestedURI, responseURI))
            {
                string resultHTML = "";
                byte[] reqHTML = ResponseAsBytes(resp);
                if (!string.IsNullOrEmpty(FetchingEncoding))
                    resultHTML = Encoding.GetEncoding(FetchingEncoding).GetString(reqHTML);
                else if (!string.IsNullOrEmpty(resp.CharacterSet))
                    resultHTML = Encoding.GetEncoding(resp.CharacterSet).GetString(reqHTML);

                resp.Close();
                fetchResult.IsOK = true;
                fetchResult.ResultHTML = resultHTML;
            }
            else
            {
                URLAddress = responseURI.AbsoluteUri;
                relayPageCount++;
                if (relayPageCount > 5)
                {
                    fetchResult.IsOK = false;
                    fetchResult.ErrorMessage = "Maximum page redirection occured.";
                    return fetchResult;
                }
                return Fetch();
            }
        }
        catch (Exception ex)
        {
            fetchResult.IsOK = false;
            fetchResult.ErrorMessage = ex.Message;
        }
        return fetchResult;
    }

any solution would greatly appreciate

© Stack Overflow or respective owner

Related posts about httpwebresponse

Related posts about c#