Goh*_*ool 8 c# parsing multipartform-data
我只是想编写一个多部分解析器,但事情变得复杂,并想知道是否有人知道C#中的现成解析器!
为了说清楚,我正在编写自己的"小"http服务器,并且需要解析多部分表单数据!
在此先感谢Gohlool
Lor*_*ori 27
我在这里开源了一个C#Http表单解析器.
这比在CodePlex上提到的另一个稍微灵活一些,因为您可以将它用于Multipart和Non-Multipart form-data,并且它还为您提供在Dictionary对象中格式化的其他表单参数.
这可以使用如下:
非多
public void Login(Stream stream)
{
string username = null;
string password = null;
HttpContentParser parser = new HttpContentParser(stream);
if (parser.Success)
{
username = HttpUtility.UrlDecode(parser.Parameters["username"]);
password = HttpUtility.UrlDecode(parser.Parameters["password"]);
}
}
Run Code Online (Sandbox Code Playgroud)
多
public void Upload(Stream stream)
{
HttpMultipartParser parser = new HttpMultipartParser(stream, "image");
if (parser.Success)
{
string user = HttpUtility.UrlDecode(parser.Parameters["user"]);
string title = HttpUtility.UrlDecode(parser.Parameters["title"]);
// Save the file somewhere
File.WriteAllBytes(FILE_PATH + title + FILE_EXT, parser.FileContents);
}
}
Run Code Online (Sandbox Code Playgroud)