Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Working on a site crawler for keeping offline disaster communications files up to date. It was working fine until I added a new site. My other sites list a path to an HTML page such as "https://www.scc-ares-races.org/aresraces.htm". I am trying to point to another site that does not use pages like that, the urls are just to the site or folder with no HTML file specified.
Trying to use the code below to try and open that site generates an exception. How does one work around this?
Using .Net 5 in a WPF application targeting Win7 (to allow most people to use)
The site comes up fine on the same machine I am developing on in Edge and Firefox

C#
using System.Net;
using System.Windows;


namespace WebRequester
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ClickMe_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                WebRequest myWebRequest;
                WebResponse myWebResponse;
                myWebRequest = WebRequest.Create(@"https://www.specsnet.org/");
                //myWebRequest.Timeout = myWebRequest.Timeout * 50;                     <<No change
                myWebResponse = myWebRequest.GetResponse();//Boom!
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.InnerException.InnerException.Message.ToString() ,ex.Message ) ;
            }
        }
    }
}


Exception details:
EX: An error occurred while sending the request.
IE: The response ended prematurely.

What I have tried:

1: Tried bumping up the WebRequest timeout. Got to 50x default, same error.
2; To eliminate funky code issues I created a new WPF application with a single button that ran the four lines above. Using that code in example now.
3: Trying targeting different OS versions with no change.
4: Changed WebResponse to HttpWebResponse. No change in behaviour.
Posted
Updated 2-Apr-22 6:26am
v5
Comments
charles henington 31-Mar-22 19:34pm    
can you show the exception you recieve? hard to even guess without seeing something other than a basic webrequest
Richard Deeming 1-Apr-22 3:46am    
If you want someone to help you fix an exception, you need to provide the full details of the exception. Click the green "Improve question" link and add the exception to your question.

NB: There's a good chance you may be getting a generic error from the server. If that's the case, there's no easy way to find out what's causing it. For example, it could be that the server code is crashing because your request doesn't include a User-Agent header.
Stiv Ostenberg 2022 1-Apr-22 23:49pm    
Thanks for the comment. No idea how to troubleshoot that, I tried to find a way to set User-Agent in the WebRequest but nothing popped out at me. Found some articles but my WebRequest did not have a UserAgent property.
Richard MacCutchan 1-Apr-22 3:50am    
I just tried that code and it worked fine. You need to provide far more detail about your code and exactly what is happening.
Richard MacCutchan 2-Apr-22 4:20am    
This must be due to something in your local network. I have just run that exact code again (from my home in the UK) and it works perfectly.

1 solution

Ignore what I posted it appears that the website is causing a System.Net.WebException, the inner exception says that the connection attempt failed because the connected party did not respond in time.

So the problem is with the site you are connecting to, I tried it in the browser and it also failed.

So the go back to the website owners for an explanation.
 
Share this answer
 
v2
Comments
Richard MacCutchan 1-Apr-22 3:59am    
See my comment above.
Tony Hill 1-Apr-22 4:04am    
My mistake, just a bit early for me.
Richard MacCutchan 1-Apr-22 4:19am    
That's odd. I am also in UK and can get to the site in the browser as well as in the sample code. What are you doing different from me?
Tony Hill 1-Apr-22 6:16am    
I tried to access it using Chrome, Firefox and Edge and none of them seem to work.

However I also tried the same three browsers on another machine and I was able to load the page.

Obviously a machine config issue, the only difference between the two machines was one is my work machine with a VPN connected and the other a personal machine with no VPN just a straight connection through a BT router.

I disconnected the VPN on the work machine and tried again and was then able to access the web page.

I am guessing the way the network bods have configured the VPN is at the bottom of the problem in this instance but I don't know if that bit of info is of any use to the OP.
Stiv Ostenberg 2022 1-Apr-22 22:53pm    
Edge and Firefox both work on the same machine I am developing on. No VPN involved. May try another network, but still hoping to figure this out...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900