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)