Web service working fine in Android but not in IOS

DarshanJSDarshanJS USMember ✭✭✭✭
edited June 2017 in Xamarin.Forms

Hi Guys, I have created simple project which invokes asmx web service in json format,

and error is_ "500, ReasonPhrase: 'Internal Server Error"_

the web service is working fine for Android and getting results in Android emulator, but the same code is working for IOS
can you please help me to resolve this

Here is the code
public async void GetCategories()
{
if (NetworkCheck.IsInternet())
{

                var client = new System.Net.Http.HttpClient();
                var formcontent = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string,string>("StrCustomerID","C001"),
                new KeyValuePair<string, string>("StrCustomerPwd","12345")
            });

                categorylist ObjContactList = new categorylist();
                var response = await client.PostAsync("http://localhost/mynewservice/WebService.asmx/GetCategories",formcontent);

                if (response.IsSuccessStatusCode)
                {
                    string Categoryjson = await response.Content.ReadAsStringAsync();


                    if (Categoryjson != "" && Categoryjson != "Invalid Entry")
                    {
                        //Converting JSON Array Objects into generic list  
                        ObjContactList = JsonConvert.DeserializeObject<categorylist>(Categoryjson);
                    }
                    //Binding listview with server response    
                    listviewcategories.ItemsSource = ObjContactList.Table;

                }
                else
                {
                    await DisplayAlert("JSONParsing", "No network is available.", "Ok");
                }
                //Hide loader after server response    
                ProgressLoader.IsVisible = false;
            }

Answers

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @DarshanJS - the most likely reason is due to your url of localhost. Check if you can reach the website in the iOS simulator in the browser.

  • DarshanJSDarshanJS USMember ✭✭✭✭

    @AdamP said:

    Yes Sir, i can browse in that IOS simulator, actually other projects are working fine in same IOS simulator,
    But the web service i have created for other projects in C# language but this i have created in VB.Net, Whether that may be the issue,

  • VelocityVelocity NZMember ✭✭✭
  • Hi @DarshanJS

    Try this in Info.plist in your Project:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    

    For reference go through this image it is from Info.plist in my project

    for more details:
    https://stackoverflow.com/questions/30731785/how-do-i-load-an-http-url-with-app-transport-security-enabled-in-ios-9

    Let me know if you have any doubts further
    ThankYou.

  • DarshanJSDarshanJS USMember ✭✭✭✭

    Dude i am not getting that option itself in my project only, can you guide me to navigate to that settings

  • Hi @DarshanJS ,

    download the attachment below, I am sending you Images with Step-by-Step Tutorial to set NSAppSecurity

    Let me know if you have any doubts
    Thank you

  • DarshanJSDarshanJS USMember ✭✭✭✭

    @AdamP said:

    i got a very strange solution
    Initially i have created the web service by coding in VB.Net and that was giving internal server error only in IOS and the method is
    SqlCmd.CommandText = SqlStrQuery.ToString
    ObjDB.GetDataSet(dsData, SqlCmd)

                HttpContext.Current.Response.Output.Write((JsonConvert.SerializeObject(dsData, Newtonsoft.Json.Formatting.Indented)))
    
                HttpContext.Current.Response.End
    

    Then i created same web method in c# it is working fine both in IOS and Android and the code is

    SqlDataAdapter sda = new SqlDataAdapter(strsql, ConfigurationManager.ConnectionStrings["BTConString"].ToString());
                DataSet das = new DataSet();
                sda.Fill(das);
                Context.Response.Output.Write(JsonConvert.SerializeObject(das, Newtonsoft.Json.Formatting.Indented));
                Context.Response.End();
                return string.Empty;
    

    Can you notice what is wrong in VB.Net code?

  • DarshanJSDarshanJS USMember ✭✭✭✭

    By referring some solutions i came to know that no need to set exceptions after IOS 9 and if you are using Enterprise version.

  • DarshanJSDarshanJS USMember ✭✭✭✭

    Above issue got resolved by adding below code in web config file in Web service

      <compilation debug="true" strict="false" explicit="true" targetFramework="4.5.2" />
      <httpRuntime targetFramework="4.5.2" />
      <webServices>
        <protocols>
          <add name="HttpGet"/>
          <add name="HttpPost"/>
        </protocols>
      </webServices>
    
Sign In or Register to comment.