首页 / C# / C#模拟客户端发送数据示例
C#模拟客户端发送数据示例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#模拟客户端发送数据示例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3633字,纯文字阅读大概需要6分钟。
内容图文
![C#模拟客户端发送数据示例](/upload/InfoBanner/zyjiaocheng/858/31e227e6cab1442db633dc5ffd176f48.jpg)
在给一些客户端做服务器端支持时,发现他们提交上来的数据大都不是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);
????????}
????}
} ?
内容总结
以上是互联网集市为您收集整理的C#模拟客户端发送数据示例全部内容,希望文章能够帮你解决C#模拟客户端发送数据示例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。