[英]Get response from aspx page
I want to achieve the most simple thing. 我想实现最简单的事情。 That is request a aspx page from ac# console application, and then the aspx page should give a string back to the c# app.
那就是从ac#控制台应用程序请求一个aspx页面,然后aspx页面应该将一个字符串返回给c#应用程序。
I have searched around alot, but couldn't really find anything :( 我已经搜索了很多,但是找不到任何东西:(
Lets say my page is called www.example.com/default.aspx. 可以说我的页面叫做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".
然后,从我的C#应用程序向该页面发出请求,然后该页面应仅返回一个字符串“ Hello”。
Below, i have wrote in pseudo code on how i believe it should be done. 下面,我用伪代码写了关于我认为应该如何做的文章。
C# app C#应用
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 .aspx代码
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
}
}
Use System.Net.WebClient class 使用System.Net.WebClient类
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);
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 ();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.