Q:

How to call a REST API from a Windows Service

I am trying to call a Rest API from a windows service.  I have never attempted this before.  I am not sure why I am not able to make this call.
My code:
    string urlParameter = "posts/1";
    var client = new HttpClient();
    client.BaseAddress = new Uri("http://jsonplaceholder.typicode.com/");
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    HttpResponseMessage response = client.GetAsync(urlParameter).Result;
    if (response.IsSuccessStatusCode)
    {
        var dataObj = response.Content.ReadAsAsync<IEnumerable<MyType>>().Result;
    }

I am getting the following error:

Message: An error occurred while sending the request.
Inner Exception Message: {"The underlying connection was closed: The
connection was closed unexpectedly."}
Inner Exception Stack Trace    at
System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult
ar)

This error is being generated on the following line:
HttpResponseMessage response = client.GetAsync(urlParameter).Result;

Any suggestions would be appreciated.
EDIT: (Updated Code)
    string urlParameter = "posts/1";
    var client = new HttpClient();
    client.BaseAddress = new Uri("http://jsonplaceholder.typicode.com/");
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    try
    {
        //var response = await client.GetAsync(urlParameter);
        var task = client.GetAsync(urlParameter);
        task.Wait();
        var response = task.Result;

        if (response.IsSuccessStatusCode)
        {
            var dataObj = response.Content.ReadAsAsync<IEnumerable<MyType>>().Result;
        }
    }
    catch (Exception ex)
    {
        string a = ex.Message;
        string b = ex.ToString();
    }

EDIT 2: (Still getting the same error)
private static async void TestAPI2()
{
    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("Get", "application/json");

        var response = await client.GetAsync("http://jsonplaceholder.typicode.com/posts/1");

        string context = await response.Content.ReadAsStringAsync();

    }
}

A:

Your problem here is that ...
var response = client.GetAsync(urlParameter);

... returns a task, which you need to wait for it to finish first.
The cleanest way to make this call is like this ...
var response = await client.GetAsync(urlParameter);

... this requires the code be run in an async method like this ...
public async Task Foo() 
{
   var response = await client.GetAsync(urlParameter);
}

... or you can simply tell the compiler to wait using ...
var task = client.GetAsync(urlParameter);
task.Wait();

var response = task.Result;

... or a more compact version might be to use a continuation like this ...
var result = await client.GetAsync(urlParameter)
    .ContinueWith(t => t.Result.Content.ReadAsAsync<IEnumerable<MyType>>())
    .Unwrap();

... this will do the request then when the request comes back asyncronously parse it for you and unwrapping the task returns the "inner result" as this code creates a Task>> and you just want the IEnumerable so awaiting the unwrapped Task gets you the result of 2 tasks being executed one after another :)
... I looked at your specific code and ran that in a new console app, please update it to this ...
 class MyType
        {
          public int userId { get; set; }
          public int id { get; set; }
          public string title { get; set; }
          public string body { get; set; }
        }

        static void TestAnApiCall()
        {
            string urlParameter = "posts/1";
            var client = new HttpClient();
            client.BaseAddress = new Uri("http://jsonplaceholder.typicode.com/");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            try
            {
                //var response = await client.GetAsync(urlParameter);
                var task = client.GetAsync(urlParameter);
                task.Wait();
                var response = task.Result;

                if (response.IsSuccessStatusCode)
                {
                    var readTask = response.Content.ReadAsAsync<MyType>();
                    readTask.Wait();
                    var dataObj = readTask.Result;
                    Console.WriteLine(JsonConvert.SerializeObject(dataObj));
                }
            }
            catch (Exception ex)
            {
                string a = ex.Message;
                string b = ex.ToString();
            }
        }

