Reprint C # winform simulate form submission data using post value transfer (winform interacts with webpages)

Source: Internet
Author: User

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.

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.