zoukankan      html  css  js  c++  java
  • 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: est.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);
            }
        }
    }
     
  • 相关阅读:
    测试用例原理以及设计方法
    软件测试方法大汇总(转)
    黑盒测试用例大集
    博客第一篇章
    什么是Shell脚本
    部署 Django
    Django 国际化和本地化
    Django与CSRF 、AJAX
    认证系统 Authentication
    Django与缓存
  • 原文地址:https://www.cnblogs.com/amylis_chen/p/9699759.html
Copyright © 2011-2022 走看看