標籤:style blog http io ar os sp for on
卿篤軍
原文地址:http://blog.csdn.net/qingdujun/article/details/41764521
最近由於項目需要實現c#提交文字及資料至伺服器,因此研究了一下c# php資料傳送;
下面用一個樣本來示範,c# post文字+圖片 ,php端接收;
post提交資料核心代碼(post資料提交)
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> /// <param name="userName">使用者名稱</param> /// <param name="userPwd">密碼</param> /// <param name="jpegPath">頭像地址</param> /// <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">路徑</param> /// <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(); } } } }
一、c#用戶端
為了方便說明,我直接簡化了,一個提交按鈕就好了。
二、需要提交的圖片
該圖片存放在俺的E盤根目錄下面~~~~~(貼吧隨便抓的一張圖片)
path = @"E:\head.jpg";
三、php服務端
接收圖片後存放至,path = @"C:\Login\log";
附錄:
c#端代碼:
c#介面簡單代碼~~~~~(該代碼可略過~~~~~)
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> /// <param name="e"></param> 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("未知錯誤."); } } }}
伺服器php端:
<?php$name = $_POST["name"]; //擷取使用者名稱$pwd = $_POST["pwd"]; //擷取密碼$head = $_POST["head"]; //擷取頭像if(!$name || !$pwd || !$head){//傳回值為2,未知錯誤echo "2";return;}else if ($name == "DooZn" && $pwd == "a12345"){$time = date("YmdHis"); //擷取時間,用來給圖片命名$path="c:\\Login\\"; //構造路徑$path.="log"."\\"; createFolder($path); //建立儲存圖片目錄檔案夾$pic=base64_decode($head); //圖片處理$filetype=".jpg";$newname=$path.$time.$filetype; $fq=fopen($newname,'w');//開啟路徑fwrite($fq,$pic); //寫入圖片fclose($fq);echo "1"; //傳回值為1,登陸成功}else{echo "0"; //傳回值為0,登陸失敗}//建立檔案夾function createFolder($path){if (!file_exists($path)){createFolder(dirname($path));mkdir($path, 0777);}}?>
原文地址:http://blog.csdn.net/qingdujun/article/details/41764521
c# post文字圖片至伺服器