tags:

views:

207

answers:

3

Hi All,

If I have a web page that does something like this,

protected void Page_Load(object sender, EventArgs e)
    {
        List<string> items = new List<string>()
        {
            "test1",
            "test2",
            "test3"
        };

        Response.Write(items);
    }

How do I get the List back out at the other end, e.g. I have some code at the other end like this,

static void Main(string[] args)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:50513/Default.aspx");

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
    }

How do I actually pull the list back out?

I have to use an asp .net page because of a restriction with a third party API that I have to use.

+1  A: 

If you are bound to using the response (E.g. can't use WCF) you can do something like this however unless you do something more that the above the response will be the type of the list, not the contents (items.ToString() will be written to the stream, you'll need to iterate the elements and write them one by one as far as I remember)

static void Main(string[] args)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:50513/Default.aspx");

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        using(var responseStreamReader = new StreamReader(response.GetResponseStream())
        {
          var response = responseStreamReader.ReadToEnd();
          //do deserialization hereh
        }
    }
Rune FS
+1  A: 

You have to serialize your list into a format that you can then deserialize back into the list on the consuming side. There are many ways to to do this in .NET. One option would be to use JSON as that format using JSON.NET. That way you aren't limited by who can consume the data. Other formats could be XML or using one of the serializers built into .NET.

Example:

Server Side:

List<string> items = new List<string>()
{
    "test1",
    "test2",
    "test3"
};

string itemsString = JsonConvert.SerializeObject(items);
Response.Write(itemsString);

Client Side:

WebClient webRequest = new WebClient()
string json = webRequest.DonwloadString("http://localhost:50513/Default.aspx");
List<string> items = JsonConvert.DeserializeObject<List<string>>(json);
Joe Doyle
Thanks for that. I may need to send over some icons too. Would Json work for that? Or would it be better to use a binary serializer? Don't know much about Json.
peter
JSON is a very simple notation for describing objects. If you have binary data, then it would probably be best to use a binary serialzier. There is also BSON, but I haven't used it. http://en.wikipedia.org/wiki/BSON
Joe Doyle
If you need to send over icons, you should send the path to the icon instead of the icon itself.
Daniel T.
+1  A: 

To expand more on Joe's answer, the best thing to do is use JSON.NET to serialize and deserialize the list. Since the web can only send strings back and forth, JSON is a natural fit for sending objects over the web. You can serialize the list like so (using JSON.NET):

List<string> items = new List<string>()
{
    "test1",
    "test2",
    "test3"
};

var json = JsonConvert.SerializeObject(items);
Response.Write(json);

This will write to the response:

["test1", "test2", "test3"]

On the receiving end, use:

var list = JsonConvert.DeserializeObject<List<string>>(json);

And you'll get your original list back. As for icons, if you can't pass a link and actually need to pass the icon itself, you can either serialize the file as a Base64-encoded string (and then decode it), or you can use BSON:

http://james.newtonking.com/archive/2009/12/26/json-net-3-5-release-6-binary-json-bson-support.aspx

I haven't done this myself though so I can't provide an example, sorry.

Daniel T.
Thanks serialising to Base64 seems to be the answer. I have some basic stuff working. Now onto the icons.
peter