最近由于项目需要实现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. Ant 入门

    参考: Ant官网     http://ant.apache.org/ 轻量级java ee企业应用实战(李刚)      Ant当前版本1.9.6      Ant基于Java     配置环境变 ...

  2. 超强的ACM题目类型总结

    转:初期: 一.基本算法:       (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.       ...

  3. 第32条:用EnumSet代替位域

    如果一个枚举类型的元素主要用在集合中,一般使用int枚举模式,将2的不同倍数赋予每个常量: public class Text { public static final int STYLE_BOLD ...

  4. 小shell函数

     whoport() {  port=$1  echo "------ who occupied port: $port ----------"  info=$(sudo lsof ...

  5. “微信应用号对行业影响”之一,app开发速来围观

    昨天,微信张小龙的一个讲话刷爆朋友圈,除了4大价值观,最后顺便提到:要推出微信应用号! 其实,价值观也就说说听听,最后顺便提到的微信应用号,才是真正的巨型炸弹. 腾讯挟6亿高粘度用户之重,号令天下,阿 ...

  6. 推荐一款java的验证码组件——kaptcha

    使用方法: 项目中导入kaptcha-2.3.jar包 在web.xml里面新增:   <!-- 登陆验证码Kaptcha --> <servlet> <servlet- ...

  7. SET QUOTED_IDENTIFIER ON

    语法  SET QUOTED_IDENTIFIER { ON | OFF }    注释  当 SET QUOTED_IDENTIFIER 为 ON 时,标识符可以由双引号分隔,而文字必须由单引号分隔 ...

  8. 转:testlink 环境搭建(傻瓜版)

    testlink 环境搭建(傻瓜版) 2011-11-24 22:23 by 虫师, 12322 阅读, 4 评论, 收藏, 编辑 今天抽了点时间把testlink 环境搭建了一下,一直觉得这东西不怎 ...

  9. 2014年度辛星css教程夏季版第二节

    第一节我们简介了一下CSS的工作流程,我相信读者会有一个大体的认识,那么接下来我们将会深入的研究一下CSS的细节问题,这些问题的涉及将会使我们的工作更加完善. *************注释***** ...

  10. js中的字典

    最近项目JS中需要建一个特殊的颜色库,需要用到类似C#中的dictionary的概念 然后一查发现JS没有dictionary 而是Array 初始化Array colorDic = new Arra ...