0

I am wanted to download a tile from osm.

You can do it by yourself, visited OpenStreetMap site:

https://www.openstreetmap.org/#map=12/41.8384/104.5730&layers=N

After getting token you can use direct link like that

https://render.openstreetmap.org/cgi-bin/export?bbox=104.49886322021486,41.725461744125646,104.64717864990236,41.951064612666684&scale=1031340&format=svg

Great, i think you understood what i want to do.

I have a c# code (All code in Main method):

 using (WebClient client = new WebClient())
            {
        Uri url = new Uri("https://render.openstreetmap.org/cgi-bin/export?bbox=104.49886322021486,41.725461744125646,104.64717864990236,41.951064612666684&scale=1031340&format=svg");
        client.Headers.Add(HttpRequestHeader.Authorization, "render.openstreetmap.org");
        client.Headers.Add("method", "GET");
        client.Headers.Add("path", "/cgi-bin/export?bbox=28.83746292791811,59.63284874301492,30.49090531073061,60.26414816040228&scale=5000000&format=svg");
        client.Headers.Add("scheme", "https");
        client.Headers.Add(HttpRequestHeader.Accept, "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        client.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
        client.Headers.Add(HttpRequestHeader.AcceptLanguage, "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7");
        client.Headers.Add(HttpRequestHeader.Cookie, "_osm_totp_token=651097");
        client.Headers.Add("sec-ch-ua", "\"Chromium\";v=\"88\", \"Google Chrome\";v=\"88\", \"; Not A Brand\";v=\"99\"");
        client.Headers.Add(HttpRequestHeader.Referer, "https://www.openstreetmap.org/");
        client.Headers.Add("sec-fetch-dest", "document");
        client.Headers.Add("sec-fetch-mode", "navigate");
        client.Headers.Add("sec-fetch-site", "none");
        client.Headers.Add("sec-fetch-user", "?1");
        client.Headers.Add("upgrade-insecure-requests", "1");
        client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.41 YaBrowser/21.2.0.1097 Yowser/2.5 Safari/537.36");
        client.QueryString.Add("bbox", "104.49886322021486,41.725461744125646,104.64717864990236,41.951064612666684");
        client.QueryString.Add("scale", "1031340");
        client.QueryString.Add("format", "svg");
         client.DownloadFile(url, "333.svg");
}

I want to download this tile, and any others, on my pc. I am copied all headers that i find in network tab in chrome, and send this responce on server. All great but file, that i am get, is trash.

No svg data, that i want.

I am open this file, and see the same:

‹      н]лn·юЯ§Xla dљГЫ’JФ=Айџ(РУP쵤F–I¶г·пМp№VN–i›єЋ­  "iМ%зц}О®вЧ?јї^V·нv·XЇ&5YWнкb}№XН&хoяып™Їшю›Ч»ЫY…#W»I=Яп7гуу»»;q§Еz;;WRКsQЗ!гчЛЕкЭР@!њуOлкnq№џOjЫlцu5oіщЧGoнЭЏлч“ZVІІM…въPIUЈJ—нХ_®Л}»­—“zєЬМ§u%ї­{ФvэцчцbягъfE&б¤Ё$О{RWчсҐУ$}их OЯSUЇЇЪя¬—лнПУэvсѕЪЯoЪI}НкjЃКьєѕЩ^ґ?m§›щв•њ.oЪ]Tњю@•‡Їх9ZpЖwЧУЭ;¶„ЮИЁА¬іgRЯl—Я}Л6ћ’э[ґ‹m‰¦xфЦХnїD­qљеx;{ыќ<сЯУW$:[o¦‹ээX
ыj·Я®ЯµгХzХѕ"Эpэу©H
блЕr±щeєџі’фў’U67M‚пg<Соf{5Ѕhm]СЏО6Sє”­е%ШZ~`   лх=гХР џbнЕt3~{іЯК~_/Vг·нm»L¦’C(ЪЈюџУtAr$Бхг°\аЛШјЄ+ФьзкLЩF4.X
Uг(ђБToHЋъШ [5Ќ0RiеЖ‰(УR6X¦ЅPБy’{aи­Џr%ҐњuUD.Н­”°:H§HоќVЭ<J
ТDсьtѕ#№фў›ЕЪ§б§wР@`9Р KaТhзµЌ‹BАЙЅй  8{’іэЉРИFjћјAуХ(WrNZгxUPн`9љ®Ј‚нгҐR[\«ВЛ ^Жr%<iгINви1pд?K~хZhЌCўтО
rЌЗёxGFіЬ€¦С&КuпIpZ`¤ИЯиaќДr\Ќ”SХ†cCrKюNYC@bг…УјXc„Ћ 

All work's fine, no idea how to fix it. Any suggestions?

  • 1
    `client.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");` - do you decompress the response? See also https://stackoverflow.com/questions/57038748/decompress-brotli-httpresponse-in-a-httpclient – CodeCaster Feb 24 '21 at 11:27
  • 3
    You can tell your client to automatically decompress responses. It's easier with HttpClient, but possible with WebClient. I've linked your question to another which explains how to do this. – canton7 Feb 24 '21 at 11:33
  • 1
    I get good data. What are you using to view file? Make sure your viewer is set to UTF8. – jdweng Feb 24 '21 at 11:41
  • 1
    @jdweng The problem is that the response is gzip-encoded. You're probably viewing it in something which is automatically un-gzipping it – canton7 Feb 24 '21 at 11:48
  • 1
    I'm using your code and then looking at the file 333.sgv which is 27 Kbytes. For some reason a shortcut was created that was 2K. The only reason your code may not be working is the default browser and you may need to add a UserAgent header to request to specify the browser. – jdweng Feb 24 '21 at 12:03
  • 3
    @jdweng The problem is obvious and has been explained. The server is returning a gzip-encoded response, and the client hasn't been configured to decompress it – canton7 Feb 24 '21 at 12:04
  • 1
    @canton : I think the default UserAgent is the default browser on machine and some browsers will automatically decompress. – jdweng Feb 24 '21 at 12:22
  • 2
    @canton [don't bother, he's trolling](https://meta.stackoverflow.com/questions/392514/what-can-i-do-about-a-user-consistently-spreading-misinformation). The user-agent header does not specify the default browser on the machine (in whatever way that may or may not (hint: it's not) relevant to the problem at hand), and the code already sets a user-agent. – CodeCaster Feb 24 '21 at 12:54
  • 2
    @CodeCaster Aye I've crossed paths with him plenty of times before, thanks. That was my "corrective comment" for the benefit of others. – canton7 Feb 24 '21 at 13:12

0 Answers0