C#模拟客户端发送数据示例

 

在给一些客户端做服务器端支持时,发现他们提交上来的数据大都不是http请求格式,因而使用Request.Form获取不到内容,今天用C#做下模拟,并演示下数据接收。

1.发送文本

1).客户端发送文本

复制内容到剪贴板程序代码
using System;
using System.IO;
using System.Net;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Post("http://localhost/Test", "木子屋", Encoding.UTF8));
            Console.ReadKey();
        }

        /// <summary>
        /// 发送文本
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Post(string url, string data, Encoding encoding)
        {
            byte[] bytes = encoding.GetBytes(data);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.ContentLength = bytes.Length;
            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(bytes, 0, bytes.Length);
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader stream = new StreamReader(response.GetResponseStream(), encoding))
            {
                return stream.ReadToEnd();
            }
        }
    }
}

2).服务器端接收文本

复制内容到剪贴板程序代码
string str = "";
using (StreamReader stream = new StreamReader(Request.InputStream))
{
    str = stream.ReadToEnd();
}

说明:只需增加request.ContentType = "application/x-www-form-urlencoded",并将文本格式改为"data=木子屋",服务器端即可用Request.Form["data"]接收。

2.上传文件

1).客户端上传文件

复制内容到剪贴板程序代码
using System;
using System.IO;
using System.Net;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(UploadFile("http://localhost/Test", @"E:\test.jpg"));
            Console.ReadKey();
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string UploadFile(string url, string file)
        {
            //1.HttpWebRequest
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            using (Stream stream = request.GetRequestStream())
            {
                using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
                {
                    byte[] buffer = new byte[4096];
                    int bytesRead = 0;
                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        stream.Write(buffer, 0, bytesRead);
                    }
                }
            }
            //2.HttpWebResponse
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return stream.ReadToEnd();
            }
        }
    }
}

2).服务器端接收文件

复制内容到剪贴板程序代码
using (FileStream file = System.IO.File.Create(Request.MapPath("test.jpg")))
{
    using (BinaryReader stream = new BinaryReader(Request.InputStream))
    {
        byte[] buffer = new byte[4096];
        int bytesRead = 0;
        while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
        {
            file.Write(buffer, 0, bytesRead);
        }
    }
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值