1

I get unusual error with page which end with dot, example:

http://www.carsireland.ie/car-dealers/county/donegal/prestige-cars-ireland-ltd.

When I open it in my Chrome Browser everything work properly, but when I try this:

WebClient client = new WebClient();
            html = client.DownloadString("http://www.carsireland.ie/car-dealers/county/donegal/prestige-cars-ireland-ltd.");

I get:

An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The remote server returned an error: (404) Not Found.

Interesting fact this happen only on .NET framework 4.

I have a lot similar links, how I can fix it?

classical312
  • 185
  • 1
  • 4
  • 11

1 Answers1

1

UPDATE: I've found that this is actually a known bug pre-.NET 4.5 where the Uri class is treating urls the same as it would paths on the desktop. On the desktop a trailing period never matters so it is stripped. Here is the answer in another Stack Overflow question: link to answer with code example

Try wrapping the string in a Uri object first like I've done below. The code below worked just fine for me.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

namespace DownloadString
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient client = new WebClient();
            Uri uri = new Uri("http://www.carsireland.ie/car-dealers/county/donegal/prestige-cars-ireland-ltd.");
            string str = client.DownloadString(uri);
            Console.WriteLine(str);
            Console.ReadKey();
        }
    }
}

Let me know how it works!

Here is a fiddle of the same code to show that it works

As you mention in your comment, this does not work in .NET 4.0. I've also tested it in 3.5 and it does not work.

Upon further investigation I see that anything below .NET 4.5 will strip the period off of the end of the url before making the request. Here is an image showing two http requests in Fiddler. They were both made using the same code but the first was compiled using .NET 4.0, & the second was using .NET 4.5.

Comparison between .NET 4.0 & .NET 4.5 requests

Community
  • 1
  • 1
james
  • 712
  • 4
  • 11
  • 29
  • I copied the code from your question and tried it again and it works too. I'm not sure why it doesn't work for you. Could you update the question with more information about the error? – james Mar 22 '16 at 22:04
  • Good Catch, but what is solution except using .NET 4.5? – classical312 Mar 22 '16 at 22:33
  • Please see my updated answer. You should find a useful example! Good luck! – james Mar 22 '16 at 22:45