locked
Xamarin Forms ios http request header encoding gets null RRS feed

  • Question

  • User294647 posted

    I am working on a mobile app using xamarin forms, the application needs to get some data from an api so i use http client to implement that. so, the http requests work fine on both Android and Windows Phone, but when i try to debug the request on IOS i get an error which of course doesn't show an exception details.. but while debugging i found that the error happens while encoding the contents of the request, so it returns NULL in the IOS case, while it returns the encoded content in both android and windows phone case. I used different ways to send the resquest and to encode the request, but got the same result. Here are my trials: the http request code1:

    **var values = new Dictionary { { "ExpiryDate", dateAndTime.ToString() }, { "IsHijri", "false" }, { "CR_No", CetNum.Text } };

                var content = new FormUrlEncodedContent(values);
    
                var response = await client2.PostAsync("https://XXXXXXX.com/api/XXXXXX", content);
    

    ** and this is the request code trial 2 :

    **var myContent = JsonConvert.SerializeObject(cert);

        var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
        var byteContent = new ByteArrayContent(buffer);
    
        byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    
        var Response = await client.PostAsync("api/MemberCertificate/GetCertificate",
    

    byteContent).ConfigureAwait(false); string res = await Response.Content.ReadAsStringAsync();** Those trials work fine and get the content encoded with other platforms, but not IOS.

    the error message i get is System.NullReferenceException: Object reference not set to an instance of an objec So, is there a different way i should be using to send an http request in the ios scenario ? any help ? Thanks.

    Thursday, January 26, 2017 9:37 AM

All replies

  • User257079 posted

    Any update on this?

    Tuesday, January 31, 2017 9:39 AM
  • User76049 posted

    Never seen this happen in iOS doing posts to HTTPS endpoints

    A code snippet from my rest helper class.

    ``` public static async Task> PostFetchList(string url, object toPost, CancellationToken ct, Dictionary headers = null) { var httpClient = new HttpClient(); HttpContent content = new StringContent(JsonConvert.SerializeObject(toPost), Encoding.UTF8, WebConstants.ContentTypeJson);

            if (!Equals(headers, null))
            {
                foreach (var header in headers)
                {
                    content.Headers.Add(header.Key, header.Value);
                }
            }
    
            var response = await httpClient.PostAsync(new Uri(url), content, ct);
    

    ```

    Tuesday, January 31, 2017 9:55 AM
  • User326542 posted

    @NMackay said: Never seen this happen in iOS doing posts to HTTPS endpoints

    A code snippet from my rest helper class.

    ``` public static async Task> PostFetchList(string url, object toPost, CancellationToken ct, Dictionary headers = null) { var httpClient = new HttpClient(); HttpContent content = new StringContent(JsonConvert.SerializeObject(toPost), Encoding.UTF8, WebConstants.ContentTypeJson);

            if (!Equals(headers, null))
            {
                foreach (var header in headers)
                {
                    content.Headers.Add(header.Key, header.Value);
                }
            }
    
            var response = await httpClient.PostAsync(new Uri(url), content, ct);
    

    ```

    Looking at the server side your post also has no JSON content. Anyway, Thanks for your help. Enjoy the weekend!

    Friday, May 26, 2017 3:07 PM