我正在尝试从我的网络服务器检索上传的文件.当客户端通过webform(随机文件)发送文件时,我需要解析请求以获取文件并进一步处理.基本上,代码如下:
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
StreamReader r = new StreamReader(request.InputStream, System.Text.Encoding.Default);
// this is the retrieved file from streamreader
string file = null;
while ((line = r.ReadLine()) != null){
// i read the stream till i retrieve the filename
// get the file data out and break the loop
}
// A byststream is created by converting the string,
Byte[] bytes = request.ContentEncoding.GetBytes(file);
MemoryStream mstream = new MemoryStream(bytes);
// do the rest
Run Code Online (Sandbox Code Playgroud)
因此,我能够检索文本文件,但对于所有其他文件,它们都已损坏.有人能告诉我如何正确解析这些HttplistnerRequests(或提供轻量级替代)?
假设我有HttpListener.它听一些端口和IP.当我发送POST请求时,它会抓住它.如何从HttpListenerRequest解析POST参数?
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
if ( request.HttpMethod == "POST" )
{
// Here i can read all parameters in string but how to parse each one i don't know
}
Run Code Online (Sandbox Code Playgroud)