Search code examples
iosxamarinnsurlsessionnsmutableurlrequestnsurlsessiondatatask

Xamarin: NSMutableUrlRequest and NSUrlRequest with POST method and headers and data?


I have a Xamarin project where I am able to make GET calls without headers to something like "reqres.in" API like:

public Task<string> GetData()
{

    TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
    NSUrl url = new NSUrl("https://reqres.in/api/users?page=2");
    NSUrlRequest request = new NSUrlRequest(url);
    NSUrlSession session = null;
    NSUrlSessionConfiguration myConfig = NSUrlSessionConfiguration.DefaultSessionConfiguration;
    //config.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover;    //for some reason this does not work!!
    myConfig.MultipathServiceType = (NSUrlSessionMultipathServiceType)2;            //but this works!!
    session = NSUrlSession.FromConfiguration(myConfig);
    NSUrlSessionTask task = session.CreateDataTask(request, (data, response, error) => {
        //Console.WriteLine(data);
        //tell the TaskCompletionSource that we are done here:
        tcs.TrySetResult(data.ToString());
    });

    task.Resume();
    return tcs.Task;

}

But I have tried lot of variations but unable to make POST call with headers and body using similar techniques. I have tried like:

public Task<string> GetDataFromInternet()
{
    TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
    NSUrl url = NSUrl.FromString("https://content.dropboxapi.com/2/files/download");

    NSUrlRequest req = new NSUrlRequest(url);
    var authToken = "Bearer my-access-token";
    if (!string.IsNullOrEmpty(authToken))
    {
        NSMutableUrlRequest mutableRequest = new NSMutableUrlRequest(url);

        try
        {
            NSMutableDictionary dic = new NSMutableDictionary();
            dic.Add(new NSString("Authorization"), new NSString(authToken));
            dic.Add(new NSString("Dropbox-API-Arg"), new NSString("{\"path\":\"/path/to/my/file.json\"}"));
            mutableRequest.Headers = dic;

            NSData body = "{\"name\":\"morpheus\", \"job\": \"leader\"}";
            mutableRequest.Body = body;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        mutableRequest.HttpMethod = "POST";
        req = (NSUrlRequest)mutableRequest.Copy();
    }



    NSUrlSession session = null;
    NSUrlSessionConfiguration myConfig = NSUrlSessionConfiguration.DefaultSessionConfiguration;
    myConfig.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover;
    session = NSUrlSession.FromConfiguration(myConfig);
    NSUrlSessionTask task = session.CreateDataTask(req, (data, response, error) =>
    {
//Console.WriteLine(data);
//tell the TaskCompletionSource that we are done here:
tcs.TrySetResult(data.ToString());
    });

    task.Resume();
    return tcs.Task;
}

But this always returns "Could not connect to server". I have verified and this URL with my "access token" and other header and body data works fine.

Please advise any way I can make this work. Thanks.

Final Fix: My code and the code written in solution by 'Junior Jiang - MSFT' are almost identical. Both works but I found out the real reason they were not working was because of 'Entitlements.plist' missing in iOS Bundle Signing options. As the 'Multipath TCP' entitlement was in this file, I found out that just selecting that option will not help. Instead, we have to specifically include that in the 'Custom Entitlement' section as shown below: enter image description here


Solution

  • Here is a sample code about POST Request:

    NSUrl nsurl = NSUrl.FromString("Your url adress");//http://www.apple.com/
    
    NSMutableUrlRequest request = new NSMutableUrlRequest(nsurl);
    request.HttpMethod = "POST";
    NSMutableDictionary dic = new NSMutableDictionary();
    dic.Add(new NSString("Content-Type"), new NSString("application/json"));
    request.Headers = dic; // add Headers
    
    request.Body = NSData.FromString("{\"version\":\"v1\", \"cityid\": \"101010100\"}"); //add body
    
    NSUrlSession session = NSUrlSession.SharedSession;
    NSUrlSessionTask task = session.CreateDataTask(request, (data, response, error) =>
    {
        Console.WriteLine("---"+response);
        Console.WriteLine("---"+ data);
        Console.WriteLine("---"+ error);
    });
    task.Resume();