C # Winform simulate form submission data using POST value transfer (Winform interacts with webpages)

Source: Internet
Author: User

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 parameters passed to the webpage by winform are 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 by using the POST method,

// Store the data returned from the web page in byte streams to byRemoteInfo.

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 by using POST value passing, which is for your reference only. ­

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.