3

I am building an Android/iOS xamarin forms app with a portable class library. I am looking for the best way to do this example inside the PCL project:

https://msdn.microsoft.com/en-us/library/456dfw4f(v=vs.110).aspx

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Create a request for the URL. 
            WebRequest request = WebRequest.Create (
              "http://www.contoso.com/default.html");
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            WebResponse response = request.GetResponse ();
            // Display the status.
           Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            //do something with the response string

            // Clean up the streams and the response.
            reader.Close ();
            response.Close ();
        }
    }
}
Ilia Stoilov
  • 632
  • 2
  • 11
  • 27

2 Answers2

6

Flurl.Http (disclaimer: I'm the author) is a Xamarin-compatible PCL that makes this sort of thing really easy:

string s = await "http://www.contoso.com/default.html".GetStringAsync();

Get it on NuGet.

Todd Menier
  • 37,557
  • 17
  • 150
  • 173
4

Just use this NuGet package instead https://www.nuget.org/packages/Microsoft.Net.Http PCL http request is implemented in it plus it supports async.

EDIT Sample produly stollen from the Hansleman's web-site.

public static async Task<HttpResponseMessage> GetTheGoodStuff() 
{
    var httpClient = new HttpClient();
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://hanselman.com/blog/");
    var response = await httpClient.SendAsync(request);
    return response;
}
Artur Shamsutdinov
  • 3,127
  • 3
  • 21
  • 39
  • I added this to my PCL project but can't really use it. Is there a simple sample source that I can use? – Ilia Stoilov Aug 05 '15 at 11:21
  • 1
    Sorry for the next dumb question, but how can I use the returned value? If I do this: `Task responseFromServer = GetTheGoodStuff();` in another function, how can I access the data from the response? – Ilia Stoilov Aug 05 '15 at 11:59
  • No worries, HttpResponseMessage responseFromServer = await GetTheGoodStuff(); HttpClient is fully async so you need to await it. To get your head around await/async please have a look on this doco https://msdn.microsoft.com/en-us/library/hh191443.aspx – Artur Shamsutdinov Aug 05 '15 at 12:12
  • If we want to add any header into the url then how it is possible using Httpclient? – Ashish-BeJovial Sep 14 '16 at 10:29
  • I had the same question @IliaStoilov. You can use `response.Content.ReadAsStringAsync();` to get the response payload. – deadlydog Sep 24 '17 at 17:09