is there a popular C# library for working HTTP? Eg

2019-02-10 16:34发布

is there a popular C# library for working HTTP? Eg simplifing working with httpwebrequest etc

For example doing http file upload with some parameters requires many lines and knowledge of Http protocol content format etc. WebClient itself do not do it.

So being new, is there a well know library that c# developers use here?

Thanks

6条回答
戒情不戒烟
2楼-- · 2019-02-10 16:47

WebClient will do it. Like:

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

If this is not enough, be more specific. What 'parameters' do you mean?

查看更多
等我变得足够好
3楼-- · 2019-02-10 16:48

Chilkat Components

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";
}
查看更多
Anthone
4楼-- · 2019-02-10 16:54

this is best answer I could determine so far:

Here's a link I found that gets close to what I was thinking of. This solves my specific requirement, however doesn't seem to be overly broad in terms of methods. Perhaps these are just the key helper methods that are required mostly above/beyond basic WebClient / HttpWebRequest classes suport? Anyway if anyone knows of a popular c# HTTP library is better known than this let me know please. Else for the moment this link is the best I can find so far that answers my questions. Thanks for all the comments to-date.

查看更多
Luminary・发光体
5楼-- · 2019-02-10 16:59
仙女界的扛把子
6楼-- · 2019-02-10 17:02

Web forms are submitted in one of two formats: application/x-www-form-urlencoded and multipart/form-data.

WebClient provides a very simple and convenient way to upload any kind of data to a website. In case of application/x-www-form-urlencoded all you have to do is to provide a NameValueCollection. In case of multipart/form-data, AFAIK, you have to create the request data yourself (which may include both files and name value pairs).


application/x-www-form-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 sets the HTTP method to "POST" and the Content-Type to "application/x-www-form-urlencoded", URL-encodes formData and uploads it to the specified uriString.


multipart/form-data

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);

This sets the Content-Type to "multipart/form-data" with the boundary used in the request data. WebClient.UploadData sets the HTTP method to "POST" and uploads the byte array to the uriString. The request data in this example contains a file file1.dat and a form parameter submit-name which is set to Larry. The format is described in RFC2388.

查看更多
叼着烟拽天下
7楼-- · 2019-02-10 17:04

Are you looking for an Ajax library, a file upload control, or both, or neither? Check out the AjaxToolkit's AsyncFileUpload.

查看更多
登录 后发表回答