0

I am new to xamarin and Rest API.

I am Trying to call Rest API from Xamrine IOS code as follow. My moto is to get List(From API) and Display in list view(Device). But it is giving me error "Bad Request". but when put same URL in POSTMAN , gives me proper response. Please help me out here.

Postman Call Image

Call from Postman

URL: http://localhost/MobileAppAPI/

Client Code:

 static List<AuditModel> _audits = new List<AuditModel>();
        public static string GetAudits()
        {
            string URL = "http://localhost/MobileAppAPI/api/Values/Get?id=1000";
            string data = CreateObject(URL);
            return APIHelper.APIHelper.CreateObject(URL);
        }
        public static string CreateObject(string URL)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "GET";
            request.ContentType = "application/json";
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

            try
            {
                using (Stream webStream = request.GetRequestStream())            
            using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
            {
                requestWriter.Write("");
            }

                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {
                            string response = responseReader.ReadToEnd();
                            return response;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("-----------------");
                Console.Out.WriteLine(e.Message);
                return e.Message;
            }
            return "";
        }

API Code:

 // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }

Exception:

Unhandled Exception:

System.Net.WebException: Error: ConnectFailure (Connection refused)

0 Answers0