最近由于项目需要实现c#提交文字及数据至服务器,因此研究了一下c# php数据传送;

下面用一个示例来演示,c# post文字+图片 ,php端接收;

post提交数据核心代码(post数据提交)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 name="userName">用户名
        ///<param name="userPwd">密码
        ///<param name="jpegPath">头像地址
        /// <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">路径
        /// <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:Loginlog;

附录:

c#端代码:

c#界面简单代码~~~~~(该代码可略过~~~~~)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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 name="e">
        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(未知错误.);
            }
        }
    }
}

c# post文字图片至服务器的更多相关文章

  1. HttpClient4的使用,模拟浏览器登陆新浪微博,发表微博和文字+图片微博

    HttpClient4,最原始的需求就是使用其来模拟浏览器想服务器发起http请求,当然,他的功能不止于此,但是我需要的就是这个功能而已,jdk也有其自带的类似的api:UrlConnection,效 ...

  2. navigation和tabbar上的文字.图片 自定义

    [[UITabBarItem appearance] setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor blackColor] ...

  3. java生成竖排文字图片

    package com.kadang.designer.web.action;import java.awt.Color;import java.awt.Font;import java.awt.Fo ...

  4. jquery 单行滚动、批量多行滚动、文字图片翻屏滚动效果代码

    jquery单行滚动.批量多行滚动.文字图片翻屏滚动效果代码,需要的朋友可以参考下. 以下代码,运行后,需要刷新下,才能加载jquery,要不然看不到效果.一.单行滚动效果 <!DOCTYPE ...

  5. 透明窗口(窗口上面文字图片等内容不透明)的实现(使用SetLayeredWindowAttributes API函数)

    透明窗口(窗口上面文字图片等内容不透明)的实现 本文讨论通过SetLayeredWindowAttributes来实现本文的目的. SetLayeredWindowAttributes的实现必须将窗口 ...

  6. <转载>使CSS文字图片div元素居中方法之水平居中的几个方法

    文字居中,文字垂直居中水平居中,图片居中,图片水平居中垂直居中,块元素垂直居中?当我们在做前端开发是时候关于css居中的问题是很常见的.情 况有很多种,不同的情况又有不同的解决方式.水平居中的方式解决 ...

  7. 使用multer搭建一个图片接收服务器

    为了测试图片上传插件的上传功能是否好用,最近尝试搭建了一个接收图片的服务器,因为图片上传的编码格式是form-data,所以我选择使用express+multer,实现过程中发现有几个需要注意的地方, ...

  8. iOS Button 上文字图片位置的设置

    1. 添加图片+文字/文字+图片 ,不分前后,图片默认在文字前边 加空格隔开 UIButton * button =[[UIButton alloc] initWithFrame:CGRectMake ...

  9. node环境使用multer搭建一个图片接收服务器

    为了测试图片上传插件的上传功能是否好用,最近尝试搭建了一个接收图片的服务器,因为图片上传的编码格式是form-data,所以我选择使用express+multer,实现过程中发现有几个需要注意的地方, ...

随机推荐

  1. (poj)3159 Candies

    题目链接:http://poj.org/problem?id=3159 Description During the kindergarten days, flymouse was the monit ...

  2. socket通信_笔记

    (socket通信) 客户端与服务器端通信问题: 我们首先要了解一个概念性的词汇:Socket socket的英文原义是“孔”或“插座”.作为进程通信机制,取后一种意思.通常也称作“套接字”,用于描述 ...

  3. NetBeans8 类编缉器及控制台中文乱码解决

    1.类编辑器中文乱码的解决: 工具-->选项-->字体和颜色-->"语法"选项卡:右侧选择字体的地方设置一个支持中文的字体,如宋体.新宋体.微软雅黑等 2.控制台 ...

  4. WPF从入门到放弃系列第一章 初识WPF

    什么是WPF WPF(Windows Presentation Foundation)是微软推出的基于Windows Vista的用户界面框架,属于.NET Framework 3.0的一部分.它提供 ...

  5. 【实用技巧】文件MD5修改方法

    方法一 利用md5修改器   更新日志:2011-10-6 22:00修正对于路径中存在空格修改无效的bug2011-10-6 20:17更新:1.回归简约界面2.直接拖拽即可捕获地址3.一键修改文件 ...

  6. C++函数转换成C#函数

            ///                        /// </param>         /// <returns></returns>    ...

  7. ExtJS4.2学习(19)在线编辑器Ext.form.HtmlEditor(转)

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-12-24/191.html --------------- ...

  8. single page

    http://msdn.microsoft.com/zh-cn/magazine/cc507641.aspx#S7 http://blog.nodejitsu.com/scaling-isomorph ...

  9. php重定向跳转

    一.用HTTP头信息 也就是用PHP的HEADER函数.PHP里的HEADER函数的作用就是向浏览器发出由HTTP协议规定的本来应该通过WEB服务器的控制指令,例如声明返回信息的类型("Co ...

  10. [dp]HDOJ4960 Another OCD Patient

    题意: 给一个n, 第二行给n堆的价值v[i], 第三行给a[i].  a[i]表示把i堆合在一起需要的花费. 求把n堆变成类似回文的 需要的最小花费. 思路: ①记忆化搜索 比较好理解... dp[ ...