Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Making HTTP Web Requests

JulienRosen.1140JulienRosen.1140 CAMember
edited November 2014 in Xamarin.Forms

Hello all. I am a brand new xamarin forms user, still on trial. I got a nice email after signing up asking if I need help (which I do) but have yet to get a response. So I am going to outline some issues here!

My goal was to make a "Portable" cross platform app. So far I am writing all of my code in the core app project, and have not much touched the platform specific projects.

I have created some xaml forms, such as an Inventory List. On load, I want to make an HTTP request to get an inventory listing from the server and display it.

Where to find some example code?
http://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/
What do you know, a write up on four (4) different ways to make HTTP calls.

First Example: Using HTTPWebRequest

First off, the assemblies required to make the example code even build is not listed. So figure that out first, then build again.

The name 'Console' does not exist in the current context

Fine, I'll remove the outputs, not important anyways, but come on, is this code sample even meant for a cross platform project?

'System.Net.WebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'System.Net.WebRequest' could be found (are you missing a using directive or an assembly reference?)
Oh.....Ok...There is no GetResponse() in the PCL HTTP library..

So unless I am missing something, the first example is rather useless. I tried to implement it using request.BeginGetResponse() but was unsuccessful. Perhaps someone could write up an example of the async style, since that seems to be what we are forced to use.

Second Example: Using RestSharp

RestSharp does not support PCL. Why is it even an example??

Third Example: Using NSUrlConnection

iOS example in the cross platform section. Ok I get that you can write your own platform specific implementations, but this is a silly place to put it.

Fourth Example: ServiceStack Client

OK say I'm willing to pay for this great client.

Install-Package : Could not install package 'ServiceStack.Text 4.0.33'. You are trying to install this package into a project that targets
'portable-net45+win+wp80+MonoAndroid10+xamarinios10+MonoTouch10', but the package does not contain any assembly references or content files that are compatible with that framework

Maybe I am missing something but the examples seem extremely unhelpful.
Here is the only code I have successfully implemented so far, which is very limiting because you cannot pass parameters.

            HttpClient hc = new HttpClient();
            contents = await hc.GetStringAsync(url);
            res = JsonConvert.DeserializeObject<List<string>>(contents);

Help! How can I pass up parameters to a webservice defined like so?

[WebMethod]
public void GetInventory(string searchText)
{
var items = new List();
//some database calls
HttpContext.Current.Response.Write(JsonConvert.SerializeObject(items));
}

Thank you!

Posts

  • JamesBaldwinsonJamesBaldwinson GBMember ✭✭

    Try installing the WebApi Client nuget package into both the PCL and platform specific projects. That should include any missing bits you require for HttpClient.

  • SKallSKall USMember ✭✭✭✭

    You could also consider using the wrapper around HttpClient I created on the XLabs project: https://github.com/XLabs/Xamarin-Forms-Labs/tree/master/src/XLabs/Web/XLabs.Web

    The PCL portion is the interface with platform specific DLL's for NET45, iOS, Android & WP8. I chose to use platform instead of using the Microsoft Async HTTP package on all of them just for WP8 support as this way you don't need to include that in other projects. You also need the serialization DLL which lets you choose serializers from Json.NET, ServiceStack (free forked version), built in runtime Json Serializer, XML & ProtoBuffer.

Sign In or Register to comment.