0

I have been using many proxies and few can be wrong/invalid proxy so timeout mostly doesn't work for it. I am not sure if dns can't resolve or what is issue exactly. I have used

    Dim req As HttpWebRequest = WebRequest.Create(url)
    req.Proxy = New WebProxy(proxy)
    req.AllowAutoRedirect = False
    req.Timeout = 10000
    req.ReadWriteTimeout = 10000
    Dim res As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)
    Dim stream As Stream = res.GetResponseStream()
    Dim streamreader = New StreamReader(stream)
    source = streamreader.ReadToEnd()
    streamreader.Close()

It works all well for valid proxies but if proxy is dead/invalid timeout doesn't work and it keeps waiting for like 5 - 10 min which i don't want. Any kind of help will be great :)

Abhay
  • 174
  • 1
  • 7
  • 1
    Your code is VB and you tag it as c#, You can set the timeout and check the proxy, if it's timeout mark it invalid. for checking proxy https://stackoverflow.com/questions/2875130/c-sharp-how-to-test-if-proxy-is-working-or-not?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Anirudha Gupta Apr 10 '18 at 16:42
  • if i use tcpclient with wrong/invalid proxy then will it work or have same issue? – Abhay Apr 10 '18 at 16:51
  • It sometime take minutes to timeout, I was having this result last time. If you found the proxy taking more time you can mark it as invalid by yourself. see this answer https://stackoverflow.com/a/1062628/713789 you can implement it with Timer class in C# – Anirudha Gupta Apr 10 '18 at 16:54
  • I am using multiple threads so won't be good choice to mark them timeout for each thread. I have used IAsyncResult interface but it's not reliable way. – Abhay Apr 10 '18 at 16:56
  • "won't be good choice to mark them timeout for each thread" How you want to sort out this, I means simply tell user that your proxy is invalid or something else ? – Anirudha Gupta Apr 10 '18 at 17:11
  • Try setting ReceiveTimeout https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.receivetimeout(v=vs.110).aspx – Anirudha Gupta Apr 10 '18 at 17:16

0 Answers0