Search Unity

Why doesn't HttpWebRequest work on iOS?

Discussion in 'iOS and tvOS' started by sonicviz, Aug 15, 2013.

  1. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    Why does HttpWebRequest not work on iOS?
    Does anyone have a sane answer to this?

    [TABLE]
    [TR="class: monoCompatibility-roweven"]
    [TD]Class: HttpWebRequest (namespace System.Net, assembly System)[/TD]
    [TD][/TD]
    [TD="class: monoCompatibility-cell"]Search MSDN[/TD]
    [TD="class: monoCompatibility-cell"] .NET 2.0[/TD]
    [TD="class: monoCompatibility-cell"] 2.0 subset[/TD]
    [TD="class: monoCompatibility-cell"] Web player[/TD]
    [TD="class: monoCompatibility-cell"] Micro[/TD]
    [/TR]
    [/TABLE]
    ref: http://docs.unity3d.com/410/Documentation/ScriptReference/MonoCompatibility.html

    Above guide implies it should, but it doesn't afaik.

    Please DO NOT mention www (I use it) UniWeb (I use it) or any other replacements (which I probably use too).
    Yes, I know it is also UnityPro in System.Net related as well.

    I just want to know why HttpWebRequest does not work when it should. It would solve a lot of porting issues.
    Or am I wrong? It does work 100% aok on iOS /Android pro?

    Eg of error (which works fine on other platforms)
    <Warning>: ExecutionEngineException: Attempting to JIT compile method 'System.Reflection.MonoProperty:GetterAdapterFrame<Mono.Security.Protocol.Tls.HttpsClientStream, bool> (System.Reflection.MonoProperty/Getter`2<Mono.Security.Protocol.Tls.HttpsClientStream, bool>,object)' while running with --aot-only.

    at System.Reflection.MonoProperty.GetValue (System.Object obj, System.Object[] index) [0x00000] in <filename unknown>:0
    at System.Net.WebConnection.Write (System.Net.HttpWebRequest request, System.Byte[] buffer, Int32 offset, Int32 size, System.String err_msg) [0x00000] in <filename unknown>:0
    at System.Net.WebConnectionStream.WriteHeaders () [0x00000] in <filename unknown>:0
    at System.Net.WebConnectionStream.SetHeaders (System.Byte[] buffer) [0x00000] in <filename unknown>:0
    at System.Net.HttpWebRequest.SendRequestHeaders (Boolean propagate_error) [0x00000] in <filename unknown>:0
    Rethrow as WebException: Error: SendFailure (Attempting to JIT compile method 'System.Reflection.MonoProperty:GetterAdapterFrame<Mono.Security.Protocol.Tls.HttpsClientStream, bool> (System.Reflection.MonoProperty/Getter`2<Mono.Security.Protocol.Tls.HttpsClientStream, bool>,object)' while running with --aot-only.
    )
    at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
    at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0


    Normally AOT errors like this can be fixed but this is in system functionality so not possible afaik
    Additionally, It seems to work fine with HTTP but is failing on HTTPS.
    But the HTTPS works fine on the other platforms.

    So to be more specific why does HttpWebRequest fail with HTTPS on iOS, but works on desktop?

    ty!
     
    Last edited: Aug 16, 2013
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  3. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
  4. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    Hi, the stacktrace from XCode - or at least the most important final part - is the same as in "my" case, so this is IMO the same issue

    As for whether it's confirmed - well they have to confirm it first which did not happen yet, but I understand that confirming it exactly in the state as submitted might not be exactly straightforward (but nothing extraordinary either :)

    But if you can, file the bugreport with repro case - another one would definitely not hurt -)


    BTW strange that nobody was missing this feature so far... the bug sits there for very long time and was fixed in Mono only recently too. It really is a showstopper :[

    ( Apparently secure connections are not very popular and most of the games/apps use either plain HTTP or plugins on iOS - which might be slowly changing especially since all the facebooks now runs almost solely on HTTPS )

    Unfortunately, I wouldn't expect it to be fixed in Unity any time soon, since merging 3 years old networking stuff with Mono is probably quite a lot of merging and probably depends on other stuff too, but we shall see

    Glad that somebody else run into this too, google searches yielded surprisingly few results
     
  5. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    >>Glad that somebody else run into this too, google searches yielded surprisingly few results

    Yeah, I found the same. I only hit it because I'm working on some things using Amazon Web Services and ran into the old "how to secure access keys" on the client issue. Following their recommended approaches (they have a few options) I've got Unity to S3 via Elastic Beanstalk running an anon Token Vending Machine with Https.

    Decided to test it on iPad and ran into the usual AOT issues, and then the Https issues which is really annoying.

    I was about to write it up as a guided tutorial with code for the Asset Store as it's a bit like a Japanese Tea Ceremony to get working and thought it might be useful. But yeah, no Https is a showstopper using this method.

    Thanks for the confirmation, I'll submit a bug report referencing this thread.
     
  6. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    Bug report filed as (Case 557843) HttpWebRequest fails when using Https on iOS

    See also case is reported as #555589:
    ref: http://forum.unity3d.com/threads/195917-SSL-TLS-socket-write-JIT-ted-on-iOS
     
    Last edited: Aug 16, 2013
  7. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
  8. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    What is the exact error you're getting ?

    ---- how ? ---- :]

    Well, this might not be necessary an Unity's fault, but rather Mono's one in this case ( if it even can be considered as such )
    If you use self-signed certificate, consider buying a a proper one signed by an proper authority / which is also better from overall security point of view since self-signed certs are easier to fake for example.. / and try to use that.

    Also, I had issues with connecting to secure layer on Windows ( but not on Mac and don't know about Android.. )
    see http://answers.unity3d.com/questions/249052/accessing-google-apis-in-unity.html

    - you might try to provide own custom ServerCertificateValidationCallback and accept the certificate from there ( in your case probably always ( which is btw not exactly good practice either ) )
    - but I'm not sure it will help in case of self-signed certs...
     
  9. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    Sorry, missed the auto notification of this. Replied to your message.
     
  10. Paul Clark

    Paul Clark

    Joined:
    Jul 12, 2012
    Posts:
    3
    I have experienced a similar issue with HttpWebRequest over HTTPS.

    In my case I was using an external library (FlickrNet) and the crash was caused by code stripping levels 'Assemblies' and 'ByteCode' cause exceptions in the FlickrNet library due to the following classes being stripped:

    System.Activator
    System.Net.WebRequest
    System.Security.Cryptography.HashAlgorithm

    This issue can be resolved by including this link.xml file in the Assets folder:

    Code (csharp):
    1. <linker>
    2.     <assembly fullname="mscorlib">
    3.         <!-- fixes System.NotSupportedException in FlickrNet.Flickr.OAuthCalculateSignature using System_Security.Cryptography.HashAlgorithm.ComputeHash -->
    4.         <namespace fullname="System.Security.Cryptography" preserve="all"/>
    5.     </assembly>
    6.     <assembly fullname="System">
    7.         <!-- fixes System.NotSupportedException in FlickrNet.Flickr.UploadData using System.Net.WebRequest.Create -->
    8.         <type fullname="System.Net.Configuration.WebRequestModuleHandler" preserve="all"/>
    9.         <type fullname="System.Net.HttpRequestCreator" preserve="all"/>
    10.         <type fullname="System.Net.FileWebRequestCreator" preserve="all"/>
    11.     </assembly>
    12.     <assembly fullname="FlickrNet">
    13.         <!-- fixes System.MissingMethodException in FlickrNet.Flickr.GetResponseNoCache using System.Activator.CreateInstance -->
    14.         <namespace fullname="FlickrNet" preserve="all"/>
    15.     </assembly>
    16. </linker>
    17.  
    This is documented in How to Deal with Stripping when Using Reflection in Optimizing the Size of the Built iOS Player in the Unity docs.

    Don't know if it affects Android builds. Hope that helps.

    Cheers
    Paul
     
  11. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    Thanks for the info!
     
  12. Graphitech

    Graphitech

    Joined:
    Sep 14, 2012
    Posts:
    10
    Sorry if I bump this post, but I've your same problem and your solution doesn't work for me.
    Could you please tell me how my working code (yes on editor on Mac and not on IOS) in HTTPWebRequest could be in WWW?

    Code (csharp):
    1.  
    2.         int lat = "0";
    3.         int lon = "0"
    4.         string login = "user";
    5.         string password = "password";
    6.  
    7.         ServicePointManager.ServerCertificateValidationCallback = Validator;
    8.         WebRequest wr = WebRequest.Create ("https://" + weburl + "?AUTH_TYPE=basic");
    9.         wr.Credentials = new NetworkCredential(login, password);
    10.         wr.ContentType = "text/json";
    11.         wr.Method = "POST";
    12.         using (var streamWriter = new StreamWriter(wr.GetRequestStream())) {
    13.         string json = "{ \"x\": " + lat + ", \"recipients\": [], \"title\": \"Title\", \"y\": " + lon + ", \"epsg\": \"4326\"}";
    14.             streamWriter.Write(json);
    15.         }
    16.        
    17.         Stream stream = wr.GetResponse ().GetResponseStream ();
    18.        
    19.         string[] tmpMessage = new StreamReader (stream).ReadToEnd ().ToString().Split('/');
    20.  
    Tanks in advance
     
  13. eric.so

    eric.so

    Joined:
    Jun 19, 2013
    Posts:
    15
    I am still having the same issue. The link.xml solution does not work for me.

    Code (csharp):
    1.  
    2. <linker>
    3.     <assembly fullname="mscorlib">
    4.         <namespace fullname="System.Security.Cryptography" preserve="all"/>
    5.     </assembly>
    6.     <assembly fullname="System.Web.Services">
    7.         <type fullname="System.Web.Services.Protocols.SoapTypeStubInfo" preserve="all"/>
    8.         <type fullname="System.Web.Services.Configuration.WebServicesConfigurationSectionHandler" preserve="all"/>
    9.     </assembly>
    10.     <assembly fullname="System">
    11.         <type fullname="System.Net.Configuration.WebRequestModuleHandler" preserve="all"/>
    12.         <type fullname="System.Net.HttpRequestCreator" preserve="all"/>
    13.         <type fullname="System.Net.FileWebRequestCreator" preserve="all"/>
    14.     </assembly>
    15.     <assembly fullname="WebAsync">
    16.         <namespace fullname="WebAsync" preserve="all"/>
    17.     </assembly>
    18. </linker>
    19.  
     
  14. eric.so

    eric.so

    Joined:
    Jun 19, 2013
    Posts:
    15
    I gave up and use "Uniweb". Works fine for me now.
     
  15. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    Uniweb uses sockets so you need both iOS and Android pro
     
  16. kizzer

    kizzer

    Joined:
    Mar 18, 2014
    Posts:
    1
    I am investigating to use httpWebRequest on iOS and Android platforms as we intend to use https and use all the verbs for rest API. Any input on this would be very appreciated.
     
  17. eric.so

    eric.so

    Joined:
    Jun 19, 2013
    Posts:
    15
    Good luck!
     
  18. omrip32

    omrip32

    Joined:
    Jan 4, 2016
    Posts:
    105
    Did anyone solve this? I'm encountering the same problems when using the 'WebClient' class over https/TLS v1.0..
    Is there any solution beside using a 3rd party plugin (I can't use the WWW class, I'm downloading using threads) ?
     
    umerPC likes this.