标签: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
标签:style blog http io ar os sp for on
原文地址:http://blog.csdn.net/qingdujun/article/details/41764521