Forum Xamarin.iOS

Web service Call Xamarin.iOS

guru_vijayguru_vijay Member ✭✭

How to call web service API in Xamarin iOS. Please share any code links.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    We could use NSUrlSession to send a post request like:

    NSUrlSession session = NSUrlSession.SharedSession;
    NSMutableUrlRequest request = new NSMutableUrlRequest(new NSUrl("url address"));
    request.HttpMethod = "POST";
    
    NSDictionary dictionary = NSDictionary.FromObjectsAndKeys(new NSObject[] { new NSString("test") },
                                                                new NSObject[] { new NSString("name") });
    NSError error;
    var requestdata = NSJsonSerialization.Serialize(dictionary, NSJsonWritingOptions.PrettyPrinted, out error);
    request.Body = requestdata;
    var task = session.CreateDataTask(request, (data, response, e) =>
    {
    
    });
    task.Resume();
    

    Here is a simple Get request:

    NSUrlSession session = NSUrlSession.SharedSession;
    NSUrlRequest request = new NSUrlRequest(new NSUrl("https://address?id=1"));
    
    var task = session.CreateDataTask(request, (data, response, e) =>
    {
    
    });
    task.Resume();
    
Sign In or Register to comment.