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