The principle is to use winfrom to simulate the form to submit data, submit the parameters to the webpage, execute code on the webpage, get the data, and then the winform program reads all the source code of the webpage, in this way, parameters and real data are transmitted between Windows applications and Web applications.
First, create a Windows application and a web application.
In a web application, the webpage is switched to the source code and some useless code in the source code is deleted. Only the header is retained. When a Windows application reads the webpage source code, these will be read together, however, these are useless data, and deleting them does not affect the operation. The code to be retained is as follows:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
Then implement the function in page_load. The following is an example of adding two numbers and returning them to winform.
String A = request. Form ["A"]. tostring (); // The parameter passed to the webpage by winform is identified by
String B = request. Form ["B"]. tostring ();
Int c = convert. toint32 (A) + convert. toint32 (B );
Response. Write (C. tostring (); // write data to the webpage
The example code for passing parameters in winform is as follows:
Private void button#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 textbox1 to a parameter identified by a and pass it to the webpage by post
Varpost. Add ("B", textbox2.text. Trim ());
// Pass all data in the parameter list varpost to http: // The requested domain name or the IIS-configured address/default. aspx,
// Store the data returned from the web page in byte streams to byremoteinfo. (Note: errors are often prompted when IIS is not configured. Hey)
Byte [] byremoteinfo = W. uploadvalues ("http: // applied domain name or address configured with IIS/default. aspx"," Post ", varpost );
String sremoteinfo = system. Text. encoding. utf8.getstring (byremoteinfo );
Label1.text = sremoteinfo;
}
In this way, C # winform simulates the form to submit data using the post value passing method, which is for your reference only.