最近由于项目需要实现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. [译]Autoprefixer:用最可行的方式处理浏览器前缀的CSS后处理器

    Autoprefixer,通过Can I Use数据库来确定哪些浏览器前缀是需要的,然后解析CSS文件,将前缀添加到CSS规则里. 你所要做的就是添加你的资源构建工具(比如:Grunt),然后你就可以 ...

  2. The method getContextPath() is undefined for the type ServletContext

    我出错时,到网上说得是版本问题,我找到对应的包javax-servlet5.1.12.jar,把它删了,居然不报错了,原来是和包servlet-2_5-api.jar冲突了

  3. myeclipse 右键 Add Struts... 页面报404 错误

    网上试了很多种方法都不对,结果老师两下点出来了 我的改正方法是: 将WebRoot/WEB-INF/web.xml中的 <url-pattern>/*</url-pattern> ...

  4. java.util.regx Demo

    import java.util.regex.Matcher;import java.util.regex.Pattern; public class TestRegex { public stati ...

  5. PHPstrom2016.1激活与汉化【2016.06.21依旧可用】

    : 目前的网络上有很多的关于PHPstrom激活的方法,但是很多都失效了,没有具体的使用日期,这个是我从其他网友那里转载过来的,具体地址忘记了: 方法如下: 需要在联网条件下,打开PHPstrom,在 ...

  6. Newtonsoft.Json随手记

    private static Newtonsoft.Json.JsonSerializerSettings CreateSettings(string dateFormat) { Newtonsoft ...

  7. javascript 弹出的窗口返回值给 父窗口

    直接上代码,有些地方可以用到: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <H ...

  8. Python基于比较的排序

    排序是算法学习中最基本的问题. 1.平均时间复杂度均为O(N2)的排序 1.1 插入排序 插入排序对少量元素的排序非常有效.工作机制就像打牌一样,为了将牌插入到已排好序的牌中,需要将牌与手中的牌从右向 ...

  9. ASP.NET Mvc Razor视图语法

    在ASP.NET MVC中有两套模版引擎,一套是ASPX,一套是Razor,从事过WebForms开发的朋友们,对于ASPX模版已经很熟悉了,下面我说一下我所熟悉的Razor模版引擎的一些语法,供大家 ...

  10. Codeforces Round #361 div2

    ProblemA(Codeforces Round 689A): 题意: 给一个手势, 问这个手势是否是唯一. 思路: 暴力, 模拟将这个手势上下左右移动一次看是否还在键盘上即可. 代码: #incl ...