简体   繁体   中英

Opening a website in default browser and pass to it POST data

Imagine a "client-server" application. There are 2 different clients available which can log in to the database, one is a standard windows application, one is a web application. Most of the functionality is available from both clients, but some of the functionality is available just from the web client.

So right now if the user is using the windows client and he needs to do something in the web client, he needs to open his default browser, open the correct URL to get to the login page, input the correct username and password, and after logging in he can continue with his work. Well users are a bit lazy, so they came with a request to make it easier, they want to add something to the windows application - something like a clickable button so that after clicking it their default web browser opens and they get automatically logged in (I have the login data in the windows application).

Well a nice task for a newbie :) I found many ways how to either create POST requests and send them to the server OR how to open the default browser, but none about how to open the browser AND send to it my POST request.

So I had an idea - I create a temp html file "on the fly" in the user's temp folder (I delete it at the end), the file includes just a form which will be automatically submitted.

public void CreateFormFile(string url, string userName, string password)
{
  using (StreamWriter sw = new StreamWriter(FormFileName))
    {
      sw.WriteLine("<html>");
      sw.WriteLine("<form id=\"frmRedirect\" action=\""+url+"\" method=\"post\">");
      sw.WriteLine("<input type=\"text\" name=\"userName\" id=\"userName\" value=\"" + userName + "\">");
      sw.WriteLine("<input type=\"text\" name=\"password\" id=\"password\" value=\"" + password+ "\">");
      //sw.WriteLine("<input type=\"submit\" name=\"submit\" value=\"submit\" id=\"submit\">");
      sw.WriteLine("<script type=\"text/javascript\">");
      sw.WriteLine("document.getElementById(\"frmRedirect\").submit();");
      sw.WriteLine("</script>");
      sw.WriteLine("</form>");
      sw.WriteLine("</html>");
    }
  }

And then in the main program I just call

CreateFormFile(config.URL, config.UserName, config.Password);
Process.Start(FormFileName); //FormFileName is a string property containing the full path to the temp file

Well the problem is it doesn't work on the "other side."

I created a test aspx on the server

protected void Page_Load(object sender, EventArgs e)
  {
    string userName = HttpContext.Current.Request["userName"];
    string password = HttpContext.Current.Request["password"];
    Response.Write("userName = " + userName);
    Response.Write("<br>password = " + password);
  }

So now, when I click the button, my browser opens, it gets correctly redirected to the correct page, but the page simply returns

userName = 
password =

Meaning the values are empty. When I was debugging it I found out that Request.HttpMethods is actually "GET" and not "POST" as it should be, and Request.InputStream is empty (Length == 0).

Any idea what am I doing wrong? I am testing it on localhost if it matters. Thanks for any help

I don't know you kind of database you have, but something like this...

When you log in (webapp or not) with the right user and password the server generates a random hash like (gbiaw783y98hdq98dgqod9pawhd) and stores it as a session somewhere on your database, only for that user. Also, returns this hash for you when you log in in the windows application. You store this string in some variable on the application and when the users clicks the button it opens the url where you supply the hash on GET method. The server then compares the hash you sent with the one that is one the database, if equals logs in the user, if not just echo some log in error.

Sorry if I was not clear enough

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