0

I want to achieve the most simple thing. That is request a aspx page from a c# console application, and then the aspx page should give a string back to the c# app.

I have searched around alot, but couldn't really find anything :(

Lets say my page is called www.example.com/default.aspx. And from my C# app, i make a request to that page, and then that page should just return a string saying "Hello".

Below, i have wrote in pseudo code on how i believe it should be done.

C# app

public static void Main(string[] args)
{
    //1. Make request to the page: www.example.com/default.aspx
    //2. Get the string from that page.
    //3. Write out the string. (Should be "Hello")
    Console.ReadLine();
}

.aspx code

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string text = "Hello";
        //1. Return the "text" variable to the client that requested this page
    }
}
Assassinbeast
  • 1,207
  • 1
  • 17
  • 33
  • Have a look here, it is an answer to a similar question but applies here as well http://stackoverflow.com/questions/3892042/create-http-post-request-and-receive-response-using-c-sharp-console-application – Bart Jun 12 '14 at 10:58

2 Answers2

4

Use System.Net.WebClient class

var html = new WebClient().DownloadString("http://www.example.com/default.aspx"));
Console.Write(html);

the web page should output the text as

Response.Write(text);
user2316116
  • 6,726
  • 1
  • 21
  • 35
  • Hey, thanks for answer, i got the string back, but i also got the whole html source code. How can i only get the string? – Assassinbeast Jun 12 '14 at 11:03
  • The page (.aspx file) should be empty (only leave first line with <%@ Page ... in it). If you add any html content in there then you will receive it as well. – user2316116 Jun 12 '14 at 11:09
1

You could do something like this:

protected void Page_Load(object sender, EventArgs e)
    {
        string text = "Hello";
        //1. Return the "text" variable to the client that requested this page

              Response.ContentType = "text/plain";

              Response.BufferOutput = false;
              Response.BinaryWrite(GetBytes(text));

              Response.Flush();
              Response.Close();
              Response.End();
    }

static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

From Console

WebRequest request = WebRequest.Create ("http://www.contoso.com/yourPage.aspx");

            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
            Stream dataStream = response.GetResponseStream ();
            StreamReader reader = new StreamReader (dataStream);
            string responseFromServer = reader.ReadToEnd ();
            Console.WriteLine (responseFromServer);

            reader.Close ();
            dataStream.Close ();
            response.Close ();
ale
  • 10,012
  • 5
  • 40
  • 49