是否有一个流行的C#库用于工作HTTP? 例如,简化使用httpwebrequest等

是否有一个流行的C#库用于工作HTTP? 例如,简化使用httpwebrequest等

例如,使用某些参数执行http文件上传需要许多行和Http协议内容格式等知识.WebClient本身不会这样做。

所以,新的,有一个知名的库,c#开发人员在这里使用?

谢谢

Web表单以两种格式之一提交: application / x-www-form-urlencodedmultipart / form-data

WebClient提供了一种将任何类型的数据上传到网站的简单方便的方法。 在application / x-www-form-urlencoded的情况下,你所要做的就是提供一个NameValueCollection 。 对于multipart / form-data ,AFAIK,您必须自己创建请求数据(可能包括文件和名称值对)。


应用程序/ x-WWW窗体-urlencoded

 NameValueCollection formData = new NameValueCollection(); formData["q"] = "c# webclient post urlencoded"; formData["btnG"] = "Google Search"; formData["hl"] = "en"; WebClient myWebClient = new WebClient(); myWebClient.UploadValues(uriString, formData); 

WebClient.UploadValues将HTTP方法设置为"POST" ,将Content-Type设置"application/x-www-form-urlencoded" ,对formData URL编码并将其uriString指定的uriString


多部分/格式数据

 string formData = @"--AaB03x Content-Disposition: form-data; name=""submit-name"" Larry --AaB03x Content-Disposition: form-data; name=""files""; filename=""file1.dat"" Content-Type: application/octet-stream Content-Transfer-Encoding: base64 " + Convert.ToBase64String( File.ReadAllBytes("file1.dat"), Base64FormattingOptions.InsertLineBreaks) + @" --AaB03x-- "; WebClient myWebClient = new WebClient(); myWebClient.Encoding = Encoding.ASCII; myWebClient.Headers.Add("Content-Type", "multipart/form-data; boundary=AaB03x"); myWebClient.UploadString(uriString, formData); 

这将Content-Type设置"multipart/form-data" ,其中包含请求数据中使用的边界。 WebClient.UploadData将HTTP方法设置为"POST"并将字节数组uriString 。 此示例中的请求数据包含文件file1.dat和设置为Larry的表单参数submit-name 。 RFC2388中描述了该格式。

WebClient 做到这一点。 喜欢:

 var c = new System.Net.WebClient(); c.UploadFile(url, filename); 

如果这还不够,那就更具体了。 你的意思是什么’参数’?

您是在寻找Ajax库,文件上传控件,还是两者兼而有之? 查看AjaxToolkit的AsyncFileUpload 。

这是我到目前为止可以确定的最佳答案:

这是我发现的一个链接 ,接近我的想法。 这解决了我的具体要求,但在方法方面似乎并不过分。 也许这些只是主要需要在基本WebClient / HttpWebRequest类支持之上/之外的关键辅助方法? 无论如何,如果有人知道一个流行的c#HTTP库比这更为人所知,请告诉我。 另外,目前这个链接是我能找到的最好的链接,可以回答我的问题。 感谢迄今为止的所有评论。

查看CodePlex上的HTML Agility Pack :

  • 主页 。
  • 文档
  • 所有下载包括源代码

这里还有关于你可能感兴趣的SO的post 。

奇尔卡特组件

http://www.example-code.com

 Chilkat.HttpRequest req = new Chilkat.HttpRequest(); Chilkat.Http http = new Chilkat.Http(); bool success; // Any string unlocks the component for the 1st 30-days. success = http.UnlockComponent("Anything for 30-day trial"); if (success != true) { MessageBox.Show(http.LastErrorText); return; } // Build an HTTP POST Request: req.UsePost(); req.Path = "/testPostHandler.asp"; req.AddParam("arg1","This is the value for arg1."); req.AddParam("arg2","This is the value for arg2."); req.AddParam("arg3","This is the value for arg3."); // Send the HTTP POST and get the response. Note: This is a blocking call. // The method does not return until the full HTTP response is received. string domain; int port; bool ssl; domain = "www.chilkatsoft.com"; port = 80; ssl = false; Chilkat.HttpResponse resp = null; resp = http.SynchronousRequest(domain,port,ssl,req); if (resp == null ) { textBox1.Text += http.LastErrorText + "\r\n"; } else { // Display the HTML page returned. textBox1.Text += resp.BodyStr + "\r\n"; }