the principle is to use the Winfrom simulation form to submit data, to submit the parameters submitted to the Web page, Web page execution code, get the data, and then winform the program to read all the source code of the page, This achieves the effect of both the communication and the real data between the Windows application and the Web application. -
start by creating a Windows application and a Web application. -
In the Web application, you switch the Web page to the source code and delete some useless code from the source code, leaving only the head, which will be read together when the Windows application reads the Web source, but these are useless data, and the deletion has no effect. The code to keep is as follows: -
<%@ page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%> -
then implement the functionality in Page_Load, which is an example of a webpage that adds two numbers and returns them to WinForm. -
String a = request.form["A"]. ToString ();//The parameters passed to the Web page by WinForm are identified with a-
String B = request.form["B"]. ToString (); -
int C = Convert.ToInt32 (A) + Convert.ToInt32 (B); -
Response.Write (C.tostring ());//write data to a Web page -
The example code for implementing the parameters in WinForm is as follows:-
private void Button1_Click (object sender, EventArgs e)-
{ -
WebClient w = new WebClient (); -
System.Collections.Specialized.NameValueCollection varpost = new System.Collections.Specialized.NameValueCollection (); -
Varpost.add ("A", TextBox1.Text.Trim ());//Convert the data in the TextBox1 to a parameter identified by a, and pass the value of the post to the Web page-
Varpost.add ("B", TextBox2.Text.Trim ()); -
//Varpost all data in the parameter list to be passed to the value of post HTTP//The domain name you have requested or the address configured with IIS/default.aspx ,
//And the data returned from the Web page is stored in a byte stream into the Byremoteinfo) (Note: IIS configuration is often not configured to prompt the error, hey)-
byte[] Byremoteinfo = w.uploadvalues ("/HTTP//Application good domain name or address/default.aspx configured with IIS", "POST ", varpost);
String sremoteinfo = System.Text.Encoding.UTF8.GetString (Byremoteinfo); -
Label1. Text = Sremoteinfo; -
} -
This enables C # WinForm to simulate form submission data by using the Post value method, which is only for your reference
Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.
C # WinForm simulates form submission data using post values (WinForm interacts with Web pages)