0

I am trying to use an API to download documents which will be in either PDF or Word document formats. The service gives directions only up to how to construct the request URL, which I have done.

The API offers an xml or json method in the url, I am using xml.

I have tried to get the string and parsing it to an XDocument:

        XDocument response;
        using (var webClient = new System.Net.WebClient())
        {
            response = XDocument.Parse(webClient.DownloadString(url));
        }
        Console.WriteLine(response);

I also tried downloading as a file:

        using (var webClient = new System.Net.WebClient())
        {
            webClient.DownloadFile(url, filepath));
        }

Both produce the same result:

%PDF-1.5
%µµµµ
1 0 obj
<</Type/Catalog/Pages 2 0 R/Lang(en-US) >>
endobj
2 0 obj
<</Type/Pages/Count 2/Kids[ 3 0 R 27 0 R] >>
endobj
3 0 obj
<</Type/Page/Parent 2 0 R/Resources<</Font<</F1 5 0 R/F2 7 0 R/F3 9 0 R/F4 11 0 R/F5 13 0 R/F6 21 0 R>>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/Annots[ 18 0 R 19 0 R 20 0 R 26 0 R] /MediaBox[ 0 0 612 792] /Contents 4 0 R/Group<</Type/Group/S/Transparency/CS/DeviceRGB>>/Tabs/S>>
endobj
4 0 obj
<</Filter/FlateDecode/Length 6592>>
stream
xœÝ=k“Û6’ß]åÿÀOYÍžÍo2ÙrÝ8‰sÙ‡Çn²Æ2=¦­¡|ó°ãÔþøën€/‘ÐH r]¥"%ˆÝht7ú…KÎïÞ:Q™H•0#R©®xš©ä²¼{çå_ïÞùßíC~ùkRÓ“åi.›B¥Fï÷Œ‡ÏîÞùôK¸I<{y÷K2ø%ŒóTfR™'Ï.îÞÉRø,ÉRžkx•Ery>ñá_ß½óÛ"9ùwòìïwï|ÏÿÑ…J‹¼A„ñ4×r®<a,Í䮺ÈS×\¤ïáZÌ"ë6Ts•*UCd
NUÃS„ªÊ¤<.UU‘š T•9)&U¥NMª
–ʸTðû Te9rRLªò•L

and a lot more like this goes on. It's very odd characters, I am not sure how to parse an XML or PDF document from this.

hsbsid
  • 301
  • 1
  • 2
  • 10
  • 4
    why would you convert it? That is a PDF. Write it to disk (name it foo.pdf) and double click on the file to open it in your pdf reader – rene Jul 03 '18 at 20:27
  • didn't realize I was getting a pdf file when the API gave me only xml and json options. thanks @rene – hsbsid Jul 04 '18 at 18:08

1 Answers1

0

You could use HttpClient for Async requests Httpclient is a better option instead of webclient

 public static async Task get()
    {
            HttpClient _httpClient = new HttpClient())
            string url = "https://someurl.com";
            var result = await _httpClient.GetAsync($"{url}");

            // contents to a log file
            string resultContent = await result.Content.ReadAsStringAsync();
            File.WriteAllText("D://dT.pdf", resultContent);
            // ... write to log
    }
Hitesh Anshani
  • 1,499
  • 9
  • 19
  • 2
    You're also using HttpClient wrong. Even though it implements IDisposable, it shouldn't be in a using statement. See [You're Using HttpClient Wrong And It's Destabilizing Your Software](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/). A better alternative would be to use a library that handles these complications for you, such as [Flurl.Http](https://flurl.io/) or [RestSharp](http://restsharp.org/). – mason Jul 03 '18 at 20:55
  • httpclient is in under using now – Hitesh Anshani Jul 03 '18 at 20:57
  • Please update answer if you want @mason i will from that aswell and this method is working properly in my case – Hitesh Anshani Jul 03 '18 at 20:58
  • 1
    That's exactly my point - you have it in a using statement. And that's dangerous. Did you even read the link I provided? – mason Jul 03 '18 at 20:58
  • @mason now its fine? – Hitesh Anshani Jul 03 '18 at 21:00