c# post文字图片至服务器
-
最近由于项目需要实现c#提交文字及数据至服务器,因此研究了一下c# php数据传送;
下面用一个示例来演示,c# post文字+图片 ,php端接收;
post提交数据核心代码(post数据提交)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475using 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{classpostClass{/// <summary>/// 向服务器post文字和图片/// </summary>///<param name="url">url///<param name="userName">用户名///<param name="userPwd">密码///<param name="jpegPath">头像地址/// <returns>返回服务器返回值</returns>publicstring post(string url,string userName, string userPwd, string jpegPath){//将图片转化为byte[]再转化为stringstring 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 =newStreamReader(stream, Encoding.GetEncoding(utf-8));//获得返回值string result = streamReader.ReadToEnd();stream.Close();#endregion//将服务器返回值返回returnresult;}/// <summary>/// 图片转为Byte字节数组/// </summary>///<param name="FilePath">路径/// <returns>字节数组</returns>privatebyte[] imageToByteArray(string FilePath){using (MemoryStream ms =newMemoryStream()){using (Image imageIn = Image.FromFile(FilePath)){using (Bitmap bmp =newBitmap(imageIn)){bmp.Save(ms, imageIn.RawFormat);}}returnms.ToArray();}}}}一、c#客户端
为了方便说明,我直接简化了,一个提交按钮就好了。

二、需要提交的图片
该图片存放在俺的E盘根目录下面~~~~~(贴吧随便抓的一张图片)
path = @E:head.jpg;

三、php服务端
接收图片后存放至,path = @C:Loginlog;
附录:
c#端代码:
c#界面简单代码~~~~~(该代码可略过~~~~~)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950using 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{publicpartialclasspostFrom : Form{publicpostFrom(){InitializeComponent();}/// <summary>/// 提交按钮,提交post数据/// </summary>///<param name="sender">///<param name="e">privatevoidbtnpost_Click(object sender, EventArgs e){//postClass为数据提交类postClass ps =newpostClass();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(登陆成功.);}elseif(value.Contains(0)){MessageBox.Show(登陆失败.);}else{MessageBox.Show(未知错误.);}}}}
c# post文字图片至服务器的更多相关文章
- HttpClient4的使用,模拟浏览器登陆新浪微博,发表微博和文字+图片微博
HttpClient4,最原始的需求就是使用其来模拟浏览器想服务器发起http请求,当然,他的功能不止于此,但是我需要的就是这个功能而已,jdk也有其自带的类似的api:UrlConnection,效 ...
- navigation和tabbar上的文字.图片 自定义
[[UITabBarItem appearance] setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor blackColor] ...
- java生成竖排文字图片
package com.kadang.designer.web.action;import java.awt.Color;import java.awt.Font;import java.awt.Fo ...
- jquery 单行滚动、批量多行滚动、文字图片翻屏滚动效果代码
jquery单行滚动.批量多行滚动.文字图片翻屏滚动效果代码,需要的朋友可以参考下. 以下代码,运行后,需要刷新下,才能加载jquery,要不然看不到效果.一.单行滚动效果 <!DOCTYPE ...
- 透明窗口(窗口上面文字图片等内容不透明)的实现(使用SetLayeredWindowAttributes API函数)
透明窗口(窗口上面文字图片等内容不透明)的实现 本文讨论通过SetLayeredWindowAttributes来实现本文的目的. SetLayeredWindowAttributes的实现必须将窗口 ...
- <转载>使CSS文字图片div元素居中方法之水平居中的几个方法
文字居中,文字垂直居中水平居中,图片居中,图片水平居中垂直居中,块元素垂直居中?当我们在做前端开发是时候关于css居中的问题是很常见的.情 况有很多种,不同的情况又有不同的解决方式.水平居中的方式解决 ...
- 使用multer搭建一个图片接收服务器
为了测试图片上传插件的上传功能是否好用,最近尝试搭建了一个接收图片的服务器,因为图片上传的编码格式是form-data,所以我选择使用express+multer,实现过程中发现有几个需要注意的地方, ...
- iOS Button 上文字图片位置的设置
1. 添加图片+文字/文字+图片 ,不分前后,图片默认在文字前边 加空格隔开 UIButton * button =[[UIButton alloc] initWithFrame:CGRectMake ...
- node环境使用multer搭建一个图片接收服务器
为了测试图片上传插件的上传功能是否好用,最近尝试搭建了一个接收图片的服务器,因为图片上传的编码格式是form-data,所以我选择使用express+multer,实现过程中发现有几个需要注意的地方, ...
随机推荐
- (poj)3159 Candies
题目链接:http://poj.org/problem?id=3159 Description During the kindergarten days, flymouse was the monit ...
- socket通信_笔记
(socket通信) 客户端与服务器端通信问题: 我们首先要了解一个概念性的词汇:Socket socket的英文原义是“孔”或“插座”.作为进程通信机制,取后一种意思.通常也称作“套接字”,用于描述 ...
- NetBeans8 类编缉器及控制台中文乱码解决
1.类编辑器中文乱码的解决: 工具-->选项-->字体和颜色-->"语法"选项卡:右侧选择字体的地方设置一个支持中文的字体,如宋体.新宋体.微软雅黑等 2.控制台 ...
- WPF从入门到放弃系列第一章 初识WPF
什么是WPF WPF(Windows Presentation Foundation)是微软推出的基于Windows Vista的用户界面框架,属于.NET Framework 3.0的一部分.它提供 ...
- 【实用技巧】文件MD5修改方法
方法一 利用md5修改器 更新日志:2011-10-6 22:00修正对于路径中存在空格修改无效的bug2011-10-6 20:17更新:1.回归简约界面2.直接拖拽即可捕获地址3.一键修改文件 ...
- C++函数转换成C#函数
/// /// </param> /// <returns></returns> ...
- ExtJS4.2学习(19)在线编辑器Ext.form.HtmlEditor(转)
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-12-24/191.html --------------- ...
- single page
http://msdn.microsoft.com/zh-cn/magazine/cc507641.aspx#S7 http://blog.nodejitsu.com/scaling-isomorph ...
- php重定向跳转
一.用HTTP头信息 也就是用PHP的HEADER函数.PHP里的HEADER函数的作用就是向浏览器发出由HTTP协议规定的本来应该通过WEB服务器的控制指令,例如声明返回信息的类型("Co ...
- [dp]HDOJ4960 Another OCD Patient
题意: 给一个n, 第二行给n堆的价值v[i], 第三行给a[i]. a[i]表示把i堆合在一起需要的花费. 求把n堆变成类似回文的 需要的最小花费. 思路: ①记忆化搜索 比较好理解... dp[ ...