0

OK, so this has kept me busy for too long. Testing the api call with the SOAPUI or Restlett in Chrome I can get the expected result - a binary that represents a pdf or possibly html. I've highlighted the part of the Url in question in yellow (this does represent a document number that should be downloaded and in the parent system is "130318/HJRWIQ" but we cannot use the '/' in the url thus the 130318%2FHJRWIQ representation of the same in the web-api call.

Chrome Restlett Api test results: Chrome - RestLet Api test

Now, trying to do the same in c# this url returns a '

404 not found

and I suspect that the %2F is somehow encoded back to a '/' again. Below is two different methods I tried (both doing the same) :

c# methods to download the same response;

internal static string DownloadData(string resourceUrl, string authorizationApi, StringBuilder sb)
    {
        using (var wc = new WebClient())
        {
            // copy data to byte[]
            wc.Headers.Add("Content-Type", "application/pdf");
            wc.Headers.Add("Authorization", authorizationApi);
            wc.Headers.Add("OData-MaxVersion", "4.0");
            wc.Headers.Add("OData-Version", "4.0");
            var data = wc.DownloadData(new Uri(@resourceUrl));
            sb.AppendLine(" url used\t" + wc.BaseAddress);
            sb.AppendLine(" querystring value\t" + wc.Encoding);
            return Convert.ToBase64String(data);
        }
    }

    internal static string DownloadData(string resourceUrl, string authorizationApi, ITracingService trace)
    {
        try
        {
            using (var wc = new WebClient())
            {
                wc.Headers.Add("ContentType", " text/plain");
                wc.Headers.Add("Authorization", authorizationApi);
                //wc.Headers.Add("Content-Disposition", "attachment; filename='82400200813_-doc.pdf'");
                //wc.Headers.Add("Transfer-Encoding", "chunked");
                return wc.DownloadString(new Uri(@resourceUrl));
            }
        }
        catch (Exception e)
        {
            trace.Trace("DOWNLOAD DATA EXCEPTION: \n" + e.Message + "\n" + e.InnerException);
        }

        return "";
    }

The Exception logged as per above :

DOWNLOAD DATA EXCEPTION: The remote server returned an error: (404) Not Found.

just ignore the string builder and tracing (this is used to debug remotely) as this runs online.

Anyone else ran into this before ? How do I get the url passed correctly ?

EDIT Even MS reproduced this and does not have a workaround answer - I moved this code to an azure service that could do the get independently and pass the result back;

Shane_Yo
  • 770
  • 8
  • 24
  • I have not touched C# in a few years but have you tried to use HtmlDecode()? – Tim Strawbridge Apr 24 '18 at 19:48
  • There are many questions about this, for example: https://stackoverflow.com/q/591694/5311735 – Evk Apr 24 '18 at 19:51
  • Would help if you captured the URL that is actually requested (you can get it from the W3C logs) and compared it to what you were expecting. How is it different? Is the %2F converted to a slash, or is there some other problem? – John Wu Apr 24 '18 at 20:08
  • Yes its a whole decode -encoding problem - %2F is coverted to slash. dont have access to their logs. – Shane_Yo Apr 24 '18 at 20:17
  • got this logged with microsoft,and seems to be the environment (online dynamics crm sandbox) that will always encode this.Have not come up with an answer yet. doing this via say console no prob. – Shane_Yo May 23 '18 at 21:05

0 Answers0