Bug 27527 - HttpClient throws "only http or https scheme is allowed" exception when used with BaseAddress
Summary: HttpClient throws "only http or https scheme is allowed" exception when used ...
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 5.1
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2015-02-28 05:29 UTC by software
Modified: 2017-06-30 02:05 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Scratch.Bxc27527.zip (30.99 KB, application/zip)
2015-03-05 13:26 UTC, Jonathan Pryor
Details


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:
RESOLVED FIXED

Description software 2015-02-28 05:29:04 UTC
When initializing a HttpClient with BaseAddress = http://something.com and, for example, POSTing a partial path "/parameters", it immidately throws an ArgumentException "Only http or https scheme is allowed.
The same code not using BaseAddress and POSTing "http://something.com/parameters" works fine.

I have dating from may 2014 that worked with the BaseAddress method.
Comment 1 Jonathan Pryor 2015-03-02 16:46:37 UTC
There's some good news and some bad news...

The good news is that this was also a bug in Mono 3.12. Consider the following:

  using System;
  using System.Net.Http;
  
  class App {
    public static void Main ( )
    {
      var c = new HttpClient {
        BaseAddress = new Uri ("http://developer.xamarin.com")
      };
      using (c) {
        Console.WriteLine ("*jonp* start!");
        try {
          var r = c.GetStringAsync ("/releases/").Result;
          Console.WriteLine ("*jonp* example.com/whatever: {0}", r);
        } catch (Exception e) {
          Console.WriteLine ("*jonp* Error: {0}", e);
        }
      }
    }
  }

On Mono 3.12, this would fail:

> $ mcs bxc-27527.cs -r:System.Net.Http && mono --debug bxc-27527.exe
> *jonp* start!
> *jonp* Error: System.AggregateException: One or more errors occurred ---> System.ArgumentException: Only http or https scheme is allowed
>   at System.Net.Http.HttpRequestMessage.set_RequestUri (System.Uri value) [0x0004c] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Net.Http/System.Net.Http/HttpRequestMessage.cs:95 
>   at System.Net.Http.HttpRequestMessage..ctor (System.Net.Http.HttpMethod method, System.Uri requestUri) [0x0000d] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Net.Http/System.Net.Http/HttpRequestMessage.cs:58 
>   at System.Net.Http.HttpRequestMessage..ctor (System.Net.Http.HttpMethod method, System.String requestUri) [0x00000] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Net.Http/System.Net.Http/HttpRequestMessage.cs:51 
>   at System.Net.Http.HttpClient.GetAsync (System.String requestUri, HttpCompletionOption completionOption) [0x00000] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:154 
>   at System.Net.Http.HttpClient+<GetStringAsync>c__async5.MoveNext () [0x00027] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:320 
>   --- End of inner exception stack trace ---
>   at System.Threading.Tasks.Task`1[System.String].get_Result () [0x00000] in <filename unknown>:0 
>   at App.Main () [0x00000] in <filename unknown>:0 
>  --> (Inner exception 0) System.ArgumentException: Only http or https scheme is allowed
>   at System.Net.Http.HttpRequestMessage.set_RequestUri (System.Uri value) [0x0004c] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Net.Http/System.Net.Http/HttpRequestMessage.cs:95 
>   at System.Net.Http.HttpRequestMessage..ctor (System.Net.Http.HttpMethod method, System.Uri requestUri) [0x0000d] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Net.Http/System.Net.Http/HttpRequestMessage.cs:58 
>   at System.Net.Http.HttpRequestMessage..ctor (System.Net.Http.HttpMethod method, System.String requestUri) [0x00000] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Net.Http/System.Net.Http/HttpRequestMessage.cs:51 
>   at System.Net.Http.HttpClient.GetAsync (System.String requestUri, HttpCompletionOption completionOption) [0x00000] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:154 
>   at System.Net.Http.HttpClient+<GetStringAsync>c__async5.MoveNext () [0x00027] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:320 

On mono/master, this works.

The bad news is that monodroid/master, despite using mono/master, fails in a *different* way:

> *jonp* Error: System.AggregateException: One or more errors occurred. ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
>   --- End of inner exception stack trace ---
>   at System.Threading.Tasks.Task.ThrowIfExceptional (Boolean includeTaskCanceledExceptions) [0x00000] in <filename unknown>:0 
>   at System.Threading.Tasks.Task`1[System.String].GetResultCore (Boolean waitCompletionNotification) [0x00000] in <filename unknown>:0 
>   at System.Threading.Tasks.Task`1[System.String].get_Result () [0x00000] in <filename unknown>:0 
>   at Scratch.Bxc27527.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00000] in <filename unknown>:0 
> ---> (Inner Exception #0) System.Threading.Tasks.TaskCanceledException: A task was canceled.<---

The above is generated from the code:

  protected override void OnCreate (Bundle bundle)
  {
    base.OnCreate (bundle);
  
    var c = new HttpClient {
      BaseAddress = new Uri ("http://developer.xamarin.com")
    };
    using (c) {
      Console.WriteLine ("*jonp* start!");
      try {
        var r = c.GetStringAsync ("/releases/").Result;
        Console.WriteLine ("*jonp* example.com/whatever: {0}", r);
      } catch (Exception e) {
        Console.WriteLine ("*jonp* Error: {0}", e);
      }
    }
  }
Comment 2 Jonathan Pryor 2015-03-02 16:49:13 UTC
@Marek: Any idea what would cause a TaskCanceledException, as seen on monodroid/master in Comment #1?
Comment 3 Marek Safar 2015-03-03 04:13:08 UTC
@jonp: could you attach full stack trace.

Same code works using mono master/4.0 in console. Perhaps this is some Android initialisation issue as there is a timeout for the request to complete and you are using .Result which is a blocking operation instead of await
Comment 4 Marek Safar 2015-03-04 13:07:28 UTC
@jonp: could you test if with following change you still get cancelation exception

			var c = new HttpClient {
				BaseAddress = new Uri ("http://developer.xamarin.com"),
				Timeout = TimeSpan.FromDays (5)
			};
Comment 5 Jonathan Pryor 2015-03-05 11:19:15 UTC
@mareks: When I make the change, GetStringAsync() still doesn't return in a "reasonable" period of time -- i.e., tens of seconds or minutes -- and I don't want to wait 5 *days* for it to timeout...

Fearing that there was a deadlock occurring, I decided to actually use async/await:

  protected override async void OnCreate (Bundle bundle)
  {
    base.OnCreate (bundle);

    // Set our view from the "main" layout resource
    SetContentView (Resource.Layout.Main);

    var c = new HttpClient {
      BaseAddress = new Uri ("http://developer.xamarin.com"),
      Timeout     = TimeSpan.FromDays (5),
    };
    using (c) {
      Console.WriteLine ("*jonp* start!");
      try {
        var r = await c.GetStringAsync ("/releases/");
        Console.WriteLine ("*jonp* example.com/whatever: {0}", r);
      } catch (Exception e) {
        Console.WriteLine ("*jonp* Error: {0}", e);
      }
    }
  }

On the plus side, I don't get an Application Not Responding dialog now.

On the minus side, as of the time of this writing (~5-10 minutes later?), GetStringAsync() *still* has not completed, and I get neither the "example.com/whatever" nor the "Error:" messages.

I suspect I need to add some logging *somewhere* within mscorlib.dll. Any thoughts on where to continue this investigation?
Comment 6 Jonathan Pryor 2015-03-05 13:26:46 UTC
Created attachment 10173 [details]
Scratch.Bxc27527.zip

Add complete test case.
Comment 7 Marek Safar 2015-03-13 11:06:04 UTC
I cannot reproduce the issue. It works for me normally. I get

*jonp* example.com/whatever: <!DOCTYPE html>
<html lang="en">

...some html....

I am using following for testing

Xamarin.Android
Version: 5.1.99.79 (Business Edition)

Xamarin Android Player
Version: Unknown version
Location: /Applications/Xamarin Android Player.app
Comment 8 Daniel Hughes 2016-01-18 20:34:00 UTC
I have just run into this error in mono 3.2.8 on Ubuntu 15.10 while initializing a HttpRequestMessage for use in a HttpClient which has a valid https base address.

Code works fine on .net 4.5 on windows.

My guess is that anyone on ubuntu 15.10 who uses the web from c# is going to have this problem. I would call it a show stopper.
Comment 9 Jonathan Pryor 2016-01-18 20:53:51 UTC
> I have just run into this error in mono 3.2.8 on Ubuntu 15.10

Mono *4.2* is the current stable release:

http://www.mono-project.com/download/
> The latest Mono release is: 4.2 Stable (4.2.1.102)

I would suggest updating your mono install:

http://www.mono-project.com/docs/getting-started/install/linux/#debian-ubuntu-and-derivatives
Comment 10 Cody Beyer (MSFT) 2017-06-30 02:05:38 UTC
Thank you for taking the time to submit this report. After reviewing the description of this bug, we believe it no longer affects the current version of Xamarin.Android. If you are still experiencing the issue after updating your packages, please reopen this report with an attached reproduction.