Bug 52682 - HttpClient.PostAsync to submit HttpContent containing FormUrlEncodedContent Fails on Xamarin.iOS NSUrlSession Cycle 9
Summary: HttpClient.PostAsync to submit HttpContent containing FormUrlEncodedContent F...
Status: VERIFIED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 10.4 (C9)
Hardware: PC Mac OS
: Normal major
Target Milestone: C9SR0
Assignee: Sebastien Pouliot
URL: https://github.com/brminnick/URLEncod...
: 52711 52750 52850 53220 ()
Depends on:
Blocks:
 
Reported: 2017-02-22 19:03 UTC by Brandon Minnick
Modified: 2017-03-16 23:58 UTC (History)
10 users (show)

Tags: Cyle9KI
Is this bug a regression?: Yes
Last known good build: Cycle 8 Stable (As of 22 February 2017)

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Brandon Minnick 2017-02-22 19:03:44 UTC
Reproduction Code (Readme included): https://github.com/brminnick/URLEncodedFormBug

This code uses HttpClient.PostAsync to submit HttpContent containing FormUrlEncodedContent to a website. If HttpClient.PostAsync is successful, the HTML response Content should contain the string Correct!.



When Does The Error Occur?

Xamarin.iOS on Cycle 9, using NSUrlSession for the HttpClient Implementation
-Cycle 9 Beta Release as of 22 February 2017



When Does the Error Not Occur?

Xamarin.iOS on Cycle 8
-Cycle 8 Stable Release as of 22 February 2017
Xamarin.iOS on Cycle 9, using Managed (default) for the HttpClient Implementation
-Cycle 9 Beta Release as of 22 February 2017
Xamarin.Android on Cycle 9 using AndroidClientHandler for the HttpClient Implementation
-Cycle 9 Beta Release as of 22 February 2017
Xamarin.Android on Cycle 9 using Managed (HttpClientHandler) for the HttpClient Implementation
-Cycle 9 Beta Release as of 22 February 2017
Creating the Post request using PostMan, link to postman collection



Steps to reproduce bug:

1. Download and open URLEncodedFormBug.sln in Xamarin Studio using the Environment Configuration below
2. Set the URLEncodedFormBug.iOS as the Startup Project
3. Build, Deploy and run URLEncodedFormBug.iOS on a Device or iOS10 Simulator
4. Click the Submit Button
5. DisplayAlert shows Failed because the HTML Response doesn't contain Correct!

Steps to run program successfully

1. Download and open URLEncodedFormBug.sln in Xamarin Studio using the Environment Configuration below
2. Right-click URLEncodedFormBug.iOS, select Options --> iOS Build
3. Change the HttpClient Implementation to Managed (default)
4. Click OK to accept changes
5. Set the URLEncodedFormBug.iOS as the Startup Project
6. Build, Deploy and run URLEncodedFormBug.iOS on a Device or iOS10 Simulator
7. Click the Submit Button
8. DisplayAlert shows Success

=== Xamarin Studio Enterprise ===

Version 6.2 (build 1812) Installation UUID: 3ac98a61-67a7-411f-b124-19833ec9a519 Runtime:

Mono 4.8.0 (mono-4.8.0-branch/9ac5bf2) (64-bit)
GTK+ 2.24.23 (Raleigh theme)
Package version: 408000489
=== NuGet ===

Version: 3.5.0.0

=== Xamarin.Profiler ===

Version: 1.1.99 Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin.Android ===

Version: 7.1.0.35 (Xamarin Enterprise) Android SDK: /Users/brandonm/Library/Developer/Xamarin/android-sdk-macosx Supported Android versions: 4.1 (API level 16) 7.0 (API level 24) 7.1 (API level 25)

SDK Tools Version: 25.2.5 SDK Platform Tools Version: 25.0.3 SDK Build Tools Version: 25.0.2

Java SDK: /usr java version "1.8.0_102" Java(TM) SE Runtime Environment (build 1.8.0_102-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin Android Player ===

Version: 0.6.5 Location: /Applications/Xamarin Android Player.app

=== Xamarin Inspector ===

Version: 1.1.1.0 Hash: 607f1ba Branch: 1.1-release Build date: Thu, 16 Feb 2017 18:57:39 GMT

=== Apple Developer Tools ===

Xcode 8.2.1 (11766.1) Build 8C1002

=== Xamarin.Mac ===

Version: 3.0.0.391 (Xamarin Enterprise)

=== Xamarin.iOS ===

Version: 10.4.0.121 (Xamarin Enterprise) Hash: 9d6e1ab Branch: cycle9 Build date: 2017-02-10 12:10:51-0500

=== Build Information ===

Release ID: 602001812 Git revision: 33e69e5df3c62c2c4084cf1b8de26fda93c5e843 Build date: 2017-02-09 12:18:09-05 Xamarin addins: 984f6bd4e491490d1cdf37352594300dab2b1597 Build lane: monodevelop-lion-cycle9

=== Operating System ===

Mac OS X 10.12.3 Darwin brandonm-mac.local 16.4.0 Darwin Kernel Version 16.4.0 Thu Dec 22 22:53:21 PST 2016 root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64
Comment 1 Vincent Dondain [MSFT] 2017-02-22 21:01:01 UTC
This is, by far, the most complete and well explained bug I've ever seen!
Congratulations and thank you.

I could indeed reproduce the issue with XI C9 `10.4.0.121` and XI master `10.5.0.478`.
Comment 2 Vincent Dondain [MSFT] 2017-02-22 21:10:16 UTC
And it's important to add that it indeed *does work* with XI cycle8 (10.3.1.8).
Given the precision of this bug report I'm sure no one doubted it.
Comment 3 Sebastien Pouliot 2017-02-22 22:55:10 UTC
@Manuel it sounds similar to #52354
Comment 4 kris 2017-02-23 09:26:18 UTC
I have exactly the same issue with application/json content type
Comment 5 Sebastien Pouliot 2017-02-24 22:20:23 UTC
*** Bug 52711 has been marked as a duplicate of this bug. ***
Comment 6 Sebastien Pouliot 2017-02-24 22:20:46 UTC
*** Bug 52750 has been marked as a duplicate of this bug. ***
Comment 7 Sebastien Pouliot 2017-02-24 22:42:16 UTC
> When Does the Error Not Occur?

+ XI 10.4 (cycle9) when using the native CFNetworkHandler

which is closer (code and performance wise) to the NSUrlSessionHandler - for anyone needing a workaround.
Comment 8 Andrew 2017-02-24 22:56:13 UTC
the CFNetworkHandler works.
Comment 9 Sebastien Pouliot 2017-02-25 04:31:55 UTC
The reason seems to be the switch from loading the request into memory (NSData) to loading it from a stream (NSInputStream).  The later is more efficient, at least for large POST'ed requests which could, in theory, not even fit in memory. 

Now that last part seems to be causing problem as it makes the native API avoids to compute Content-Length, which it normally does [1], and switch to chunked encoding (like mentioned in #52711).

[1] https://devforums.apple.com/message/919330#919330
Comment 10 Sebastien Pouliot 2017-02-25 18:51:37 UTC
PR master https://github.com/xamarin/xamarin-macios/pull/1772
Comment 11 Sebastien Pouliot 2017-02-27 19:49:35 UTC
PR merged in master https://github.com/xamarin/xamarin-macios/commit/65f39da8e677d96adb0ab8693680e378021295ae

A build (untested from master) is available from https://jenkins.mono-project.com/view/Xamarin.MaciOS/job/xamarin-macios-builds-master/ in case anyone ants to test the change.
Comment 12 Sebastien Pouliot 2017-02-27 20:38:51 UTC
*** Bug 52850 has been marked as a duplicate of this bug. ***
Comment 13 Sebastien Pouliot 2017-02-27 21:19:48 UTC
PR d15-1 https://github.com/xamarin/xamarin-macios/pull/1778 (identical to 1772)
Comment 15 Andrew 2017-03-03 23:01:57 UTC
I am noticing problems with the CFNetworkHandler and certain get request.  It works sometimes and other hangs waiting for a response.  I am going back to the ManagedHandler.
Comment 16 Ruben Buniatyan 2017-03-04 14:31:27 UTC
I confirm the fix from the master branch works for me as expected.
Comment 17 Sebastien Pouliot 2017-03-04 18:08:17 UTC
@Ruben thanks for confirming!

@Andrew, please feel a separate bug report for CFNetworkHandler so this can be investigated and fixed asap. Thanks!
Comment 19 Sebastien Pouliot 2017-03-16 13:01:23 UTC
*** Bug 53220 has been marked as a duplicate of this bug. ***