1

I am trying to set a policy for leaving unescaped character %2f ,while using urls before sending a hammock rest request.as some .net developers may know, there are encoding problems ,while trying to use unescaped characters in the PUBLIC url string for calling the linkedin api.

App.config is out of the question as its a phone project.

reflection is another workaround suggested on avrious sites including this one.

while using one of the solutions,

      var getSyntaxMethod =
            typeof(UriParser).GetMethod("GetSyntax", BindingFlags.Static | 
              BindingFlags.NonPublic);
        if (getSyntaxMethod == null)
        {
            throw new MissingMethodException("UriParser", new Exception("GetSyntax"));
        }
        var uriParser = getSyntaxMethod.Invoke(null, new object[] { "http" });

        var flagsField =
            uriParser.GetType().BaseType.GetField("m_Flags", BindingFlags.Instance | 
                 BindingFlags.NonPublic);
        if (flagsField == null)
        {
            throw new MissingFieldException("UriParser", new Exception("m_Flags"));
        }
        int oldValue = (int)flagsField.GetValue(uriParser);
        oldValue += 4096;
        flagsField.SetValue(uriParser, oldValue);

I am getting the following error.-

     Additional information: Attempt by method                
     'PhotonWorld.View.LinkedinAuthPage.AddAllowAnyOtherHostFlagToHttpUriParser()' to   
      access method 'System.UriParser.GetSyntax(System.String)' failed

i guess this is specific to phone .i am using wp8.

any one else had issues?

wbsat
  • 61
  • 6

1 Answers1

2

Yes, this seems due to limitation of silverlight, including silverlight for windows phone version :

"In Silverlight, you cannot use reflection to access private types and members. If the access level of a type or member would prevent you from accessing it in statically compiled code, you cannot access it dynamically by using reflection". [MSDN : Security Considerations for Reflection]

I tested demonstration code from this post :

var surl = "http://x/y./z";

var url = new Uri(surl);
Console.WriteLine("Broken: " + url.ToString());

MethodInfo getSyntax = typeof(UriParser).GetMethod("GetSyntax", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
FieldInfo flagsField = typeof(UriParser).GetField("m_Flags", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (getSyntax != null && flagsField != null)
{
    foreach (string scheme in new[] { "http", "https" })
    {
        UriParser parser = (UriParser)getSyntax.Invoke(null, new object[] { scheme });
        if (parser != null)
        {
            int flagsValue = (int)flagsField.GetValue(parser);
            // Clear the CanonicalizeAsFilePath attribute
            if ((flagsValue & 0x1000000) != 0)
                flagsField.SetValue(parser, flagsValue & ~0x1000000);
        }
    }
}

url = new Uri(surl);
Console.WriteLine("Fixed: " + url.ToString());

that's working fine in console application, but throwing exception :

Attempt by method .... to access method 'System.UriParser.GetSyntax(System.String)' failed

when trying to invoke the private method in Windows Phone application (both WP7 and WP8).

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
  • Thanks for your work...my project is just windows phone 8 sdk,c#,xaml...though I guess the Silverlight code base is used for windows phone 8 SDK?...so what is the final solution for getting linked details thru public urls for phone developers? any more ideas since all doors seem shut ? – wbsat Mar 20 '14 at 14:38
  • I was just *surprised* finding such limitation in MSDN and tried to prove it. Yes windows phone is based on sliverlight, hence I stated *silverlight for windows phone*. – har07 Mar 20 '14 at 15:05
  • Anyway, I can't find working solution so far. This seems an old bug that has been fixed in standard .NET framework (`var url = new Uri("http://x/y./z");` yield correct url without those reflection works in console apps, but the same code cause dots removed in WP apps). I'd suggest to open new question specific asking about this different behavior on 2 platforms (or I'll do later, I'm also curious). – har07 Mar 20 '14 at 15:10