开发者

Response.Write(object)

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 us开发者_运维技巧e.


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.


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
        }
    }


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜