在给一些客户端做服务器端支持时,发现他们提交上来的数据大都不是http请求格式,因而使用Request.Form获取不到内容,今天用C#做下模拟,并演示下数据接收。
1.发送文本
1).客户端发送文本
2).服务器端接收文本
说明:只需增加request.ContentType = "application/x-www-form-urlencoded",并将文本格式改为"data=木子屋",服务器端即可用Request.Form["data"]接收。
2.上传文件
1).客户端上传文件
2).服务器端接收文件
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();
}
}
}
}
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();
}
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();
}
}
}
}
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);
}
}
}
{
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);
}
}
}