多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > web前端 > htmlcss > c# post文字圖片至服務(wù)器

c# post文字圖片至服務(wù)器

來(lái)源:程序員人生   發(fā)布時(shí)間:2014-12-08 09:02:53 閱讀次數(shù):5137次

作者:卿篤軍

原文地址:http://blog.csdn.net/qingdujun/article/details/41764521


最近由于項(xiàng)目需要實(shí)現(xiàn)c#提交文字及數(shù)據(jù)至服務(wù)器,因此研究了1下c# php數(shù)據(jù)傳送;

下面用1個(gè)示例來(lái)演示,c# post文字+圖片 ,php端接收;

需要添加:using system.web;  

如果你的VS2010中右側(cè)援用欄.NET里面沒(méi)有,可以在以下目錄中查找該.dll添加進(jìn)來(lái)便可

path = @"C:WindowsMicrosoft.NETFrameworkv2.0.50727"

post提交數(shù)據(jù)核心代碼(post數(shù)據(jù)提交)

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> /// 向服務(wù)器post文字和圖片 /// </summary> /// <param name="url">url</param> /// <param name="userName">用戶名</param> /// <param name="userPwd">密碼</param> /// <param name="jpegPath">頭像地址</param> /// <returns>返回服務(wù)器返回值</returns> public string post(string url,string userName, string userPwd, string jpegPath) { //將圖片轉(zhuǎn)化為byte[]再轉(zhuǎn)化為string string array = Convert.ToBase64String(imageToByteArray(jpegPath)); //構(gòu)造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()) { //提交數(shù)據(jù) reqStream.Write(bytePara, 0, para.Length); } //獲得服務(wù)器返回值 HttpWebResponse httpWebResponse = (HttpWebResponse)httpWeb.GetResponse(); Stream stream = httpWebResponse.GetResponseStream(); StreamReader streamReader = new StreamReader(stream, Encoding.GetEncoding("utf⑻")); //取得返回值 string result = streamReader.ReadToEnd(); stream.Close(); #endregion //將服務(wù)器返回值返回 return result; } /// <summary> /// 圖片轉(zhuǎn)為Byte字節(jié)數(shù)組 /// </summary> /// <param name="FilePath">路徑</param> /// <returns>字節(jié)數(shù)組</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(); } } } }

1、c#客戶端

為了方便說(shuō)明,我直接簡(jiǎn)化了,1個(gè)提交按鈕就行了。



2、需要提交的圖片

該圖片寄存在俺的E盤根目錄下面~~~~~(貼吧隨意抓的1張圖片)

path =  @"E:head.jpg";



3、php服務(wù)端

接收?qǐng)D片后寄存至,path = @"C:Loginlog";


附錄:

c#端代碼:

c#界面簡(jiǎn)單代碼~~~~~(該代碼可略過(guò)~~~~~)

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數(shù)據(jù) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnpost_Click(object sender, EventArgs e) { //postClass為數(shù)據(jù)提交類 postClass ps = new postClass(); string url = @"http://localhost/login.php"; string name = "DooZn"; string pwd = "a12345"; string jpegPath = @"E:head.jpg"; //提交數(shù)據(jù) string value = ps.post(url,name,pwd,jpegPath); //value為服務(wù)器返回值 if (value.Contains("1")) { MessageBox.Show("登陸成功."); } else if (value.Contains("0")) { MessageBox.Show("登陸失敗."); } else { MessageBox.Show("未知毛病."); } } } }
服務(wù)器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"); //獲得時(shí)間,用來(lái)給圖片命名 $path="c:Login"; //構(gòu)造路徑 $path.="log".""; createFolder($path); //創(chuàng)建保存圖片目錄文件夾 $pic=base64_decode($head); //圖片處理 $filetype=".jpg"; $newname=$path.$time.$filetype; $fq=fopen($newname,'w'); //打開(kāi)路徑 fwrite($fq,$pic); //寫入圖片 fclose($fq); echo "1"; //返回值為1,登陸成功 } else { echo "0"; //返回值為0,登陸失敗 } //創(chuàng)建文件夾 function createFolder($path) { if (!file_exists($path)) { createFolder(dirname($path)); mkdir($path, 0777); } } ?>
原文地址:http://blog.csdn.net/qingdujun/article/details/41764521


生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 一级网| seba51久久精品 | 999精品影视在线观看 | 国产美女网站视频 | 成年人网站免费视频 | 日本一本在线视频 | 亚洲经典激情春色另类 | 欧美春色 | 久久久久久国产精品三级 | 成人在线视频网站 | 秋霞午夜视频在线观看 | 亚洲日韩精品欧美一区二区一 | 国产成人久久久精品一区二区三区 | 国产一区二区三区久久 | 男人看片网站 | 一区二区在线不卡 | 久久91久久91精品免费观看 | 欧美高清免费一级在线 | 欧美高清乌克兰精品另类 | 欧美性狂丰满性猛交 | 在线偷拍自拍 | 四虎在线永久精品高清 | a久久久久一级毛片护士免费 | 宇都宫紫苑野外中文字幕 | 国产麻豆视频在线观看 | 俺去俺来也www色官网免费的 | 爱爱www在线观看视频高清 | 亚洲色图欧美色 | 在线观看视频一区二区三区 | 最新99国产成人精品视频免费 | 日本不卡在线一区二区三区视频 | 亚洲人成网站在线观看播放青青 | 91精品福利一区二区 | 在线观看麻豆 | 在线视频一区二区三区在线播放 | 免费尤物视频 | 欧美 日韩 成人 | 成年人网站在线观看视频 | 国产成人精品日本亚洲网站 | 久久国产精品老人性 | 亚洲欧美自拍另类图片色 |