C # transmits data to WebServer using the WebClient Post method

Source: Internet
Author: User

Help a friend to do a simple transmission of data through the web example, Baidu has copied a paragraph of code, complete, the effect is as follows:

Where TextBox1 inside is the client needs to transfer the past data, TextBox2 inside is the received return data.

The code is as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Net;namespacewebpost{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {            stringPoststring ="data1="+ TextBox1.Text +"&data2=b";//here is the parameters passed, you can use the tool to grasp the package analysis, but also can analyze themselves, mainly in the form of each name to add in            byte[] PostData = Encoding.UTF8.GetBytes (poststring);//coding, especially Chinese characters, in advance to see how to crawl the page encoding            stringURL ="http://localhost/WebTest1/Receive.aspx";//AddressWebClient WebClient =NewWebClient (); WEBCLIENT.HEADERS.ADD ("Content-type","application/x-www-form-urlencoded");//To take the post must be added to the header, if you change to get a way to remove this sentence can be            byte[] responsedata = Webclient.uploaddata (URL,"POST", PostData);//get a stream of returned characters            stringsrcstring = Encoding.UTF8.GetString (responsedata);//decodingTextBox2.Text =srcstring; }    }}

The server-side ASP code is as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; Public Partial classreceive:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {Response.Write (request.form["data1"]); Response.Write ("Roger Ok! .");    Response.End (); }}

WebClient code copied from: http://blog.163.com/len_sa/blog/static/2102540932012782222526/

Thanks to the original author.

C # transmits data to WebServer using the WebClient Post method

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.