0

I'm using this to hook my client application into my web service.
In addition, I'm looking at MSDN about reading GetResponse as per the first link.

Here's the code that I've got so far:

    public ActionResult Index()
    {
        WebRequest request = WebRequest.Create("http://localhost:49474/api/Store/Get");
        request.Method = "GET";
        WebResponse response = request.GetResponse();

        Stream stores = response.GetResponseStream();
        Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

        StreamReader sr = new StreamReader(stores, encode);
        Char[] read = new Char[1024];

        int count = sr.Read(read, 0, 1024);
        List<Store> storesList = new List<Store>();

        while (count > 0)
        {
            // need to read the contents of the response strem into the above instantiated list of stores.
        }
    }

My API delivers data like this:

    public HttpResponseMessage Get()
    {
        List<Store> stores = db.Stores.ToList();

        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, stores);

        return response;
    }

Frankly I'm not sure where to go next. The MSDN link writes it all to a string but my concerns are:

  • How am I to know how many characters I need to read at a time for each record?
  • How do I read the data sent by the API so that I can use it in my view?
Community
  • 1
  • 1
Ortund
  • 8,095
  • 18
  • 71
  • 139
  • Are you open to using 3rd party libraries? If so, you might want to try something like [RestSharp](http://restsharp.org/) – AJ Richardson Apr 22 '15 at 23:52
  • I assume I use this in the client application that I'm writing now that consumes the data from the web service? – Ortund Apr 22 '15 at 23:54
  • Yes, you use it on the client side. It will automatically read and deserialize the response data for you, as long as it was serialized as JSON or XML. – AJ Richardson Apr 23 '15 at 00:00

1 Answers1

1

You can use sr.ReadToEnd() to get the whole response string. At least you'll be able to set a breakpoint and look at that in the debugger to make sure you're getting back what you expect. Then either write a function to parse your result into some kind of class, or use a 3rd-party library to parse them if they're in a standard format; i.e. if the results are JSON then use Json.NET.

Note you can also use the built-in async operations if blocking is a concern (usually it's not for simple client apps, but still it would be a good way to learn).

Dax Fohl
  • 10,654
  • 6
  • 46
  • 90
  • So here's what the service is sending... Can you confirm this result type is `application/json`? http://i.imgur.com/0GX3VMK.png – Ortund Apr 23 '15 at 00:32
  • @Ortund I'm not sure exactly what that result means. Looks like something already parsed out by (complete guess) WCF, or something else I've not used before. I'd imagine you should be able to do `var stores = (List)response.Content.Value` or something like that, to get your desired results. – Dax Fohl Apr 23 '15 at 01:00
  • It's from ASP.NET Web API. It hasn't serialized the object yet, but it looks like it's going to serialize it as JSON because the `Content-Type` is `application/json` and it's using the `JsonMediaTypeFormatter`. – AJ Richardson Apr 23 '15 at 01:06
  • 1
    @AJRichardson oh I see that was a server-side snip. So Ortund yes, it appears that all is set up for JSON serialization, but it's always possible the server may have some config setting that overrides that at the last second. The only way to tell for sure is to set the breakpoint on the client and see what you're getting back. If indeed it's JSON, then grab JSON.NET library and look into how to parse it. – Dax Fohl Apr 23 '15 at 01:15
  • Using WebClient.DownloadString, I get this: `{"Id":1,"StoreName":"Test Store 1","APIKey":"this","Password":"p455w0rD","Address":"here","ContactPerson":"Ortund","ContactNumber":"212-555-4240","TellerCount":3,"DateRegistered":"2015-04-23T03:25:49.2490667+02:00"},` That's a full item as stored by the web service. Looks like JSON to me – Ortund Apr 23 '15 at 01:31
  • @Ortund so there you go. You should be able to use JSON.NET to take it from there. – Dax Fohl Apr 23 '15 at 01:36