C # Post text picture to server

Source: Internet
Author: User

Recently, because the project needs to implement C # submit text and data to the server, so the study of C # PHP data transfer;

Below is an example to demonstrate, C # post text + picture, PHP side receive;

Post submission Data core code (post data submission)

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475 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    {        /// <summary>        /// 向服务器post文字和图片        /// </summary>        ///<param name="url">url        ///<param name="userName">用户名        ///<param name="userPwd">密码        ///<param name="jpegPath">头像地址        /// <returns>返回服务器返回值</returns>        public string post(string url,string userName, string userPwd, string jpegPath)        {            //将图片转化为byte[]再转化为string            string array = Convert.ToBase64String(imageToByteArray(jpegPath));            //构造post提交字段            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())            {                //提交数据                reqStream.Write(bytePara, 0, para.Length);            }            //获取服务器返回值            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWeb.GetResponse();            Stream stream = httpWebResponse.GetResponseStream();            StreamReader streamReader = new StreamReader(stream, Encoding.GetEncoding(utf-8));            //获得返回值            string result = streamReader.ReadToEnd();            stream.Close();            #endregion            //将服务器返回值返回            return result;        }        /// <summary>        /// 图片转为Byte字节数组        /// </summary>        ///<param name="FilePath">路径        /// <returns>字节数组</returns>        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();            }        }    }    }

One, C # client

To make it easier to explain, I simply simplified, and a submit button was just fine.

Second, the need to submit pictures

The picture is stored in my e-packing directory under the ~~~~~ (paste a random picture)

Path = @e:head.jpg;

Third, PHP service side

After receiving the picture, store it to, path = @c:loginlog;

Appendix:

C # side code:

C # Interface Simple code ~~~~~ (this code can be skipped ~~~~~)

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 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();        }        /// <summary>        /// 提交按钮,提交post数据        /// </summary>        ///<param name="sender">        ///<param name="e">        private void btnpost_Click(object sender, EventArgs e)        {            //postClass为数据提交类            postClass ps = new postClass();            string url = @http://localhost/login.php;            string name = DooZn;            string pwd = a12345;            string jpegPath = @E:head.jpg;            //提交数据            string value = ps.post(url,name,pwd,jpegPath);            //value为服务器返回值            if (value.Contains(1))            {                MessageBox.Show(登陆成功.);            }            else if (value.Contains(0))            {                MessageBox.Show(登陆失败.);            }            else            {                MessageBox.Show(未知错误.);            }        }    }}

C # Post text picture to server

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.