0

I have the following server application in C#

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string function = context.Request.QueryString["function"];

            Methods m = new Methods();

            if (function.Equals("GetAllCreditCards"))
            {
                ArrayList text = m.GetAllCreditCards();
                context.Response.Write(text);
            }
        }

Basically, when the GetAllCreditCards method is called, it populates an arraylist with data from a database. I checked the method well and it works perfeclty.

I have a client application to which the ArrayList text is sent:

public String returnResponse(HttpWebResponse response)
        {
            StreamReader stream = new StreamReader(response.GetResponseStream());
            String answer = stream.ReadToEnd();
            return answer;
        }

            if (TextBox_Function.Text.Equals("GetAllCreditCards"))
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:4000/Handler.ashx?function=" + TextBox_Function.Text);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                String answer = returnResponse(response);
                Session["answer"] = answer;
                Response.Redirect("Results.aspx");
            }

Now, how can the client code be modified so that I extract the ArrayList from the HttpWebResponse? I am currently in the learning phase so please bear with me. Thank you.

The contents of the answer string are currently:

System.Collections.ArrayList
Matthew
  • 4,477
  • 21
  • 70
  • 93

1 Answers1

1

You should serialize your return value somehow. For ex,

context.Response.Write(new new JavaScriptSerializer().Serialize(text));

You can also use other serializers like: XmlSerializer, DataContractSerializer etc.

But As I commented in your previous question. There are better ways to write RESTful services(WCF, WebService MVC etc.). You don't need to reinvent the wheel.

I4V
  • 34,891
  • 6
  • 67
  • 79
  • Thanks :) I will look into RESTful services :) – Matthew Apr 15 '13 at 14:26
  • @Matthew At least you can use the idea in [this question](http://stackoverflow.com/questions/10017564/url-mapping-with-c-sharp-httplistener) to write it in a more generic way, if you insist on developing your framework. – I4V Apr 15 '13 at 14:41