0

I want my program to download tiles and open a map, as I click on the Tile I want to download, it gives me "The request was aborted: Could not create SSL/TLS secure channel" exception. It accepts and shows links like https://google.com eventhough it doesnt show the site itself it shows the way I want it to, black around and in the middle the Tile. I looked for alternatives like httpClient or RestSharp but none of them seems to work. It goes to the excepton from bytes = WebClient...etc So bytes stays Null.

       try
        {

            System.Net.WebClient webClient = new System.Net.WebClient();
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string blackTile = "X�V7��\u009da4�Sϥzu";
            byte[] bytes;
            int tries = 0;
            do
            {
                tries++;
                bytes = webClient.DownloadData("https://" + Request["server"] + ".tile.openstreetmap.de/tiles/osmde/" + Request["z"] + "/" + Request["x"] + "/" + Request["y"] + ".png");
                if (Request["q"] != null && Request["q"] != "")
                {
                    MemoryStream msIn = new MemoryStream(bytes);
                    MemoryStream msOut = new MemoryStream();
                    Bitmap bmp1 = new Bitmap(msIn);
                    ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
                    System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
                    EncoderParameters myEncoderParameters = new EncoderParameters(1);
                    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, Convert.ToInt64(Request["q"]));
                    myEncoderParameters.Param[0] = myEncoderParameter;
                    bmp1.Save(msOut, jgpEncoder, myEncoderParameters);
                    bytes = msOut.ToArray();
                }
            } while (tries<=5 && System.Text.Encoding.Default.GetString(md5.ComputeHash(bytes)).Equals(blackTile));


            
            HttpResponse response = Context.Response;
            response.ContentType = "image/png";
            response.AddHeader("title", "Bild");
            response.OutputStream.Write(bytes, 0, bytes.Length);
        }
        catch (Exception ex) { }
    }
Oggy
  • 1
  • 1

1 Answers1

0

I just encountered the same issue revisiting an old project that was downloading openstreetmap tiles using System.Net.WebClient. The solution was that I needed to specify TLS1.2 (or rather prevent the default use of TLS1.0). I won't steal the thunder of the answers on this site that provided the solution. I used the answer for .net 3.5 found in this answer If you are using .net 4.0 or later you could the solution in this answer instead

I expect this all boils down to the internet at large gradually killing off the insecure protocols that old versions of the .net framework will use by default if you don't tell it not to.

MNB
  • 479
  • 6
  • 12