欢迎光临

木子屋

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

作者: dnawo

日期: 2013-03-11 02:37 PM

分类: Win编程

内容: 在给一些客户端做服务器端支持时，发现他们提交上来的数据大都不是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)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.727 ms

返回

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

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.727 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.758 ms

返回