lundi 5 octobre 2015

how to get string array values from a stream?

I'm trying to return a string array from the server-side and retrieve it from the client-side.this is how my server-side looks like:

[HttpGet]
public string[] GetFileNameList()
{
    //get file names from the Directory
    string[] files = Directory.GetFiles(@"C:\Users\ndmendis\Desktop\files\in", "*.txt");
    return files;
}

this is how my client-side looks like:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:59831/Home/GetFileNameList/");
    request.ContentType = "application/text";
    request.Proxy = null;
    request.KeepAlive = false;
    //request.ContentLength = 0;
    request.Method = "GET";

    var response = request.GetResponse();
    // Get the stream containing content returned by the server.
    Stream dataStream = response.GetResponseStream();
    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader(dataStream, Encoding.UTF8);
    // Read the content.
    string responseFromServer = reader.ReadToEnd();
    String[] str = responseFromServer.Cast<string>().Cast<string>().ToArray();

From the client-side, i'm trying to the access the server over http, and it works. It goes to my server-side. And the server-side is returning the string array perfectly. But the issue is I'm not getting the proper content of that string array(My 'responseFromServer' variables value is string[]). 'responseFromServer' variable should contain the values i'm passing. Can someone please address the issue i'm having? thank you!




Aucun commentaire:

Enregistrer un commentaire