0

I'm trying to set up a Xamarin Project (for the first time) in Visual Studio 2013.

I'm using a Portable Class Library for my API consumer classes, which (among other methods) contains a GET method like this:

    private static async Task<string> Get(string endpoint)
    {

        try
        {
            var client = new HttpClient();
            client.BaseAddress = new Uri(API_URL);
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
            var response = client.GetAsync(endpoint).Result;
            string content = await response.Content.ReadAsStringAsync();
            return content;
        }
        catch (Exception ex)
        {
            var t = ex.Message;
        }

        return null;
    }

The API is set up on a publicly available server with Azure. I've tested the API from a Web-project; and it works just fine. However, when I run the iOS app through VS13 and Xamarin Build host on my Mac, I get an exception when trying to connect to the API (using the excat same method in the Portable Class Library). The exception is:

System.Net.WebExceptionStatus.ConnectFailure

with no further description. I've tested that the API is accessiable from the Mac that acts as build host, which makes me wonder, if there are some conditions for connecting to an API through an iOS app, that I don't know about.

I don't have any experience bulding Apps, and this is my first try. As I said; the API works just fine when accessed through a Web-project. What am I missing here?

MichaelCleverly
  • 2,473
  • 1
  • 24
  • 33

3 Answers3

1

Following Michael's comment on my earlier response, it appears the solution is to tune your OS X Firewall, possibly as in this related question: How do I get the Mac OS X Firewall to permanently allow my iOS app?

Community
  • 1
  • 1
BernardV
  • 1,700
  • 20
  • 15
0

Have you checked if you can connect to any other service? Maybe try with google for example. If it doesn't work then I would also check this:

var response = client.GetAsync(endpoint).Result;

as this thing can also be trouble maker. If dealing with async Tasks you shouldn't use them like that but await them(and so you get result):

var response = await client.GetAsync(endpoint);
Immons
  • 257
  • 1
  • 11
  • Thanks for your answer. I tried executing the code directly from the Mac in Xamarin studio, and it works from there. I noticed that when I'm doing it that way, my Mac asks me if I want to allow network connections from the iPhone Simulator. This prompt does not show when I run it through the build host, and I figure that that's the problem. – MichaelCleverly Jul 14 '15 at 09:03
0

Using System.net.Http.HttpClient from the full-fledged .net framework seem to be your problem. As you can see, it reduces the targets of your project when you reference that assembly.

System.net.http reference

You'd be better of using HttpWebRequest which is part of the Base Class Library (BCL) and would work with Android/iOs/WinPhone.

Try this:

    private WebRequest _request;

    private void DoRequest(string endpoint)
    {
        _request = WebRequest.Create(new Uri(endpoint))
        _request.BeginGetResponse(RequestCallback, null);
    }

    private void RequestCallback(IAsyncResult result)
    {
        Stream stream = _request.EndGetResponse(result).GetResponseStream();
        // Process your stream
        ...
    }

There is also some info about the issue with using HttpClient here: http://forums.xamarin.com/discussion/7719/using-httpclient

I hope that helps. And I'm also don't have much experience with it, so don't take anything for granted :-)

BernardV
  • 1,700
  • 20
  • 15
  • Thanks for your answer. I'm actually using a portable Http library, that is made specifically for PCL and ports to both Android and iOS. So that's not the issue. I tried executing the code directly from the Mac in Xamarin studio, and it works from there. I noticed that when I'm doing it that way, my Mac asks me if I want to allow network connections from the iPhone Simulator. This prompt does not show when I run it through the build host, and I figure that that's the problem. – MichaelCleverly Jul 14 '15 at 09:02