简体   繁体   中英

How do I increase the performance of HttpWebResponse on HTTPS Requests?

I am building an application that highly relies on the loading speed of a web page.

I am not getting any good results with HttpWebResponse on C#. I am getting better results with internet browsers like Chrome and IE

Here are the stats that i collected:

HttpWebResponse (C#) = 17 Seconds / 20 Requests

Javascript/iFrame on Chrome = 9 seconds / 20 requests

Javascript/iFrame on IE = 11 seconds / 20 requests

Question #1 Is there anything i can do, to optimize my code for better performance?

Question #2 I can click start button twice and open two connections, so that i can get on par with browser performance. This works great, however the website i send a request to has a limit. If i send a new request before the other one is completed, it blocks my connection for 10 minutes. Is there a way i can prevent this?

My Thread:

void DomainThreadNamecheapStart()
{
    while (stop == false)
    {
        foreach (string FromDomainList in DomainList.Lines)
        {
            if (FromDomainList.Length > 1)
            {
                // I removed my api parameters from the string
                string namecheapapi = "https://api.namecheap.com/foo" + FromDomainList + "bar";

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(namecheapapi);
                request.Proxy = null;
                request.ServicePoint.Expect100Continue = false;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream());

                status.Text = FromDomainList + "\n" + sr.ReadToEnd();

                sr.Close();
            }
        }
    }
}

My Button:

private void button2_Click(object sender, EventArgs e)
{
    stop = false;
    Thread DomainThread = new Thread(new ThreadStart(DomainThreadNamecheapStart));
    DomainThread.Start();
}

My Old Question: How do I increase the performance of HttpWebResponse?

You're creating a thread every time the button is pressed. Creating a thread is expensive and takes time by itself. Try using a thread from an existing thread pool (try QueueUserWorkItem ) and see if that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM