1
\$\begingroup\$

I have this method :

private string GetCheckResultFor(string url)
 {
 Stopwatch reveil = new Stopwatch();
 reveil.Start();
 HttpWebResponse response = null;
 url = Regex.Replace(url, @"\s+", "");
 if (!url.StartsWith("http") && !url.StartsWith("https")) url = "http://" + url;
 string res = "";
 try
 {
 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
 request.Method = "GET";
 response = (HttpWebResponse)request.GetResponse();
 if (response.ResponseUri.ToString() == url) res = "No Action";
 else res = "Redirect to " + response.ResponseUri.ToString();
 }
 catch (WebException e)
 {
 if (e.Status == WebExceptionStatus.ProtocolError)
 {
 response = (HttpWebResponse)e.Response;
 res = "Errorcode: " + (int)response.StatusCode;
 }
 else
 {
 res = "Error: " + e.Status;
 }
 }
 finally
 {
 if (response != null)
 {
 response.Close();
 }
 }
 reveil.Stop();
 Debug.WriteLine("For this url : " + url + " = " + reveil.ElapsedMilliseconds + " ms");
 return res;
 }

It takes between 200 and 700 ms. I know it depends on internet connection speed but I need to improve it because I only need the reponse url. I have Three possiblities :

  • Reponse Url = request Url ==> No action
  • Redirect to another url
  • Request Error

So How can I edit this snippet to reduce method response time ?

BCdotWEB
11.4k2 gold badges28 silver badges45 bronze badges
asked Apr 5, 2017 at 12:42
\$\endgroup\$
1
  • 2
    \$\begingroup\$ request.Method = "GET"; -> request.Method = "HEAD"; might save you some time and should still work. \$\endgroup\$ Commented Apr 5, 2017 at 13:47

1 Answer 1

1
\$\begingroup\$

I found that a webclient with custom settings gave me the fastest result. Personally I was building a framework for web crawling & scraping, where you want it to do the requests quickly.

Might be an alternative to check out.

public class DefaultWebClient : WebClient {
 public DefaultWebClient() {
 ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
 ServicePointManager.DefaultConnectionLimit = Int32.MaxValue;
 ServicePointManager.Expect100Continue = false;
 }
 protected override WebRequest GetWebRequest(Uri address) {
 var request = base.GetWebRequest(address) as HttpWebRequest;
 if (request != null) {
 //This might work in your case, it didnt do the trick for me.
 //request.Method = WebRequestMethods.Http.Head;
 request.Proxy = null;
 request.Timeout = 5000;
 request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
 }
 return request;
 }
}

This, in combination with retrieving the response url should do the trick.

 protected override WebResponse GetWebResponse(WebRequest request) {
 var response = base.GetWebResponse(request);
 if (response != null) {
 var responseUri = response.ResponseUri;
 // Redirect found.
 }
 else {
 // No Action.
 }
 return response;
 }

If you do a lot of requests to get the redirects, it might be a good idea to raise it as an event.

BCdotWEB
11.4k2 gold badges28 silver badges45 bronze badges
answered Apr 13, 2017 at 11:17
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.