C # post text image to server
Recently, because the project needs to implement c # To submit text and data to the server, we have studied c # php data transmission;
The following example is used to demonstrate that c # post text + images are received by the php client;
Core code of post data submission (post data submission)
Using System; using System. collections. generic; using System. linq; using System. text; using System. IO; using System. drawing; using System. web; using System. net; namespace postpic {class postClass {////// Post text and images to the server //////Url ///User name ///Password ///Avatar address ///
Return server return value
Public string post (string url, string userName, string userPwd, string signature path) {// Convert the image to byte [] and then Convert it to string array = Convert. toBase64String (imageToByteArray (shadowpath); // construct the post submission field string para = name = + userName + & pwd = + userPwd + & head = + HttpUtility. urlEncode (array); # region HttpWebRequest: HttpWebRequest httpWeb = (HttpWebRequest) WebRequest. create (url); httpWeb. timeout = 20000; httpWeb. method = POST; httpWeb. contentType = application/x-www-form-urlencoded; byte [] bytePara = Encoding. ASCII. getBytes (para); using (Stream reqStream = httpWeb. getRequestStream () {// submit data reqStream. write (bytePara, 0, para. length);} // get the server return value HttpWebResponse httpWebResponse = (HttpWebResponse) httpWeb. getResponse (); Stream stream = httpWebResponse. getResponseStream (); StreamReader streamReader = new StreamReader (stream, Encoding. getEncoding (UTF-8); // obtain the returned value string result = streamReader. readToEnd (); stream. close (); # endregion // return the return result returned by the server ;}////// Convert the image to a Byte array //////Path ///
Byte array
Private byte [] imageToByteArray (string FilePath) {using (MemoryStream MS = new MemoryStream () {using (Image imageIn = Image. fromFile (FilePath) {using (Bitmap bmp = new Bitmap (imageIn) {bmp. save (MS, imageIn. rawFormat) ;}} return ms. toArray ();}}}}
I. c # Client
For ease of instruction, I simplified it directly, just a submit button.
Ii. images to be submitted
This image is stored in the root directory of the edisk ~~~~~ (Paste a picture at Will)
Path = @ E: head.jpg;
Iii. php server
Save the received image to path = @ C: Loginlog;
Appendix:
C # End code:
C # simple interface code ~~~~~ (This code can be skipped ~~~~~)
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace postpic {public partial class postFrom: Form {public postFrom () {InitializeComponent ();}////// Submit button to submit post Data /////////Private void btnpost_Click (object sender, EventArgs e) {// postClass is the data submission class postClass ps = new postClass (); string url = @ http: // localhost/login. php; string name = DooZn; string pwd = a12345; string export Path = @ E: head.jpg; // submit data string value = ps. post (url, name, pwd, direction path); // value indicates the server return value if (value. contains (1) {MessageBox. show (LOGIN successful .);} else if (value. contains (0) {MessageBox. show (Login Failed .);} else {MessageBox. show (unknown error .);}}}}