-2

I'm consuming WCF based web services for an android app. Previously the web application (for which webservices have been written) was using .NET framework 3.5, recently it was migrated to .net framework 4.6. The below pieces of code are throwing the exception :

"Error: NameResolutionFailure at System.Net.HttpWebRequest.EndGetResponse"

url = https://121.242.223.199/SEZOnlineWebService/SezOnlineWebService.svc/FetchNumberOfSEZandUnits/1

 private async Task<JsonValue> FetchErrAsync(string url)
        {

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
            request.ContentType = "application/json";
            request.Method = "GET";


            using (WebResponse response = await request.GetResponseAsync())
            {


                using (Stream stream = response.GetResponseStream())
                {

                    JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));

                                        return jsonDoc;
                }
            }
            }

The webservices are up and running. Json format data is being displayed in a normal web browser, however from the android app, we are getting the above exception. Note: This code was working fine when the web application was running on .NET framework 3.5

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Sagar Das
  • 128
  • 9

2 Answers2

1

Is Newtonsoft.JSON supported for .net Framework 4.6 in Xamarin.Android?

Yes it is supported for .net Framework 4.6 in Xamarin.Android.

You can convert your stream to string and then use Newtonsoft.JSON to convert the string to object.

"Error: NameResolutionFailure at System.Net.HttpWebRequest.EndGetResponse"

This error is not about the Newtonsoft.JSON, it is about the network environment. By testing your url . (https://121.242.223.199/SEZOnlineWebService/SezOnlineWebService.svc/FetchNumberOfSEZandUnits/1), I find the secure issue with certificate, I think you can try bypass certificate validation with the ServerCertificateValidationCallback, and try again.

I have get your json string successfully by following code:

 public class MainActivity : Activity
    {
        Button bt1;
        TextView tv1;
        TextView tv2;
        TextView tv3;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            bt1 = FindViewById<Button>(Resource.Id.button1);
            tv1 = FindViewById<TextView>(Resource.Id.textView1);
            tv2 = FindViewById<TextView>(Resource.Id.textView2);
            tv3 = FindViewById<TextView>(Resource.Id.textView3);
            bt1.Click += Bt1_Click;
        }

        private async void Bt1_Click(object sender, EventArgs e)
        {
            await FetchErrAsync("http://121.242.223.199/SEZOnlineWebService/SezOnlineWebService.svc/FetchNumberOfSEZandUnits/1");
        }
        public bool MyRemoteCertificateValidationCallback(System.Object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            bool isOk = true;
            // If there are errors in the certificate chain, look at each error to determine the cause.
            if (sslPolicyErrors != SslPolicyErrors.None)
            {
                for (int i = 0; i < chain.ChainStatus.Length; i++)
                {
                    if (chain.ChainStatus[i].Status != X509ChainStatusFlags.RevocationStatusUnknown)
                    {
                        chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
                        chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
                        chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
                        chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
                        bool chainIsValid = chain.Build((X509Certificate2)certificate);
                        if (!chainIsValid)
                        {
                            isOk = false;
                        }
                    }
                }
            }
            return isOk;
        }
        private async Task FetchErrAsync(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
            request.ContentType = "application/json";
            request.Method = "GET";
            ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
            using (WebResponse response = await request.GetResponseAsync())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    //JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
                    //return jsonDoc;
                    StreamReader reader = new StreamReader(stream);
                    string text = reader.ReadToEnd();
                    tv1.Text = text;
                    var myFetchNumberOfSEZandUnitsResultguage = JsonConvert.DeserializeObject<MyFetchNumberOfSEZandUnitsResultguage>(text);
                    tv2.Text = myFetchNumberOfSEZandUnitsResultguage.FetchNumberOfSEZandUnitsResult[0].Key;
                    tv3.Text = myFetchNumberOfSEZandUnitsResultguage.FetchNumberOfSEZandUnitsResult[0].Value;
                }
            }
        }


    }
    public class MyFetchNumberOfSEZandUnitsResultguage
    {
        public List<MyKeyValue> FetchNumberOfSEZandUnitsResult { get; set; }
    }

    public class MyKeyValue
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }

screen shot :

enter image description here

Mike Ma
  • 2,017
  • 1
  • 12
  • 17
0

To deserialize your response to a specific object you could use:

NewtonSoft.Json.JsonConvert.DeserializeObject<MyClass>(webResponseInString);

Also a big note: WCF isn't fully supported in the Xamarin stack, so be careful when using WCF.

Mittchel
  • 1,896
  • 3
  • 19
  • 37
  • I have used the same format for deserialization as you have suggested, but still no luck! – Sagar Das Feb 13 '17 at 12:25
  • http://stackoverflow.com/questions/8999616/httpwebrequest-nameresolutionfailure-exception-in-net-with-mono-on-ubuntu – Mittchel Feb 13 '17 at 12:27
  • Thanks for sharing the post Mittchel, but we are not using host name for url, rather we are using the ip address directly. url= https://121.242.223.199/SEZOnlineWebService/SezOnlineWebService.svc/FetchNumberOfSEZandUnits/1 And this issue cannot be due to my internet connection, since the code is working fine if I use the old web services url (web application built on .net 3.5) – Sagar Das Feb 13 '17 at 12:42