简体   繁体   中英

getting incoming http request in asp.net C#

I have an external application (written from a different company) which fires some requests to my asp.net web application when hitting a button. My application is running on https://localhost:59917/Main.aspx . If I hit the request button in the external program it says the following:

08:53:25 Requesting web page : https://localhost:59917/Main.aspx?token=mcQYVqoe7LxmBx7jgHBq6FtXXp4uPCzX0FDZStiZDFMDd4l6oB3x5CgysXJKgy2y

So this app is now requesting my web application. I now have to get the given arguments of this request (in this example I need to read the argument token ). I found this thread and tried the following in my code behind (Main.aspx.cs):

protected void btnListener_Click(object sender, EventArgs e)
{
    string token = Request.QueryString["token"];
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertToken", "alert('" + token + "');", true);
}

But this returns an empty string. Since I'm new to asp.net and web development I'm not sure if I understand the HttpRequest class correctly. Is this even the right way to get the arguments I need?

If the external application makes a request to the main.aspx page then a Page_Load events occurs in the Page Lifecycle

Thus you can simply move your code in the Page_Load event handler already written for you by the designer

protected void main_Load(object sender, EventArgs e)
{
    string token = Request.QueryString["token"];
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertToken", "alert('" + token + "');", true);
}

Actually Page_Load events are called everytime a request for the page comes trough the ASP.NET engine also when you click a button on the same page.

Before the call to the event handler for the button clicked you receive a call to the Page_Load event handler. You can discover if this is the first time that your page loads looking at the IsPostBack property (true when the load is the result of an action performed on the same page, false when the page is loaded to present its interface to the end-user)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM