views:

459

answers:

6

Hi

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

+2  A: 

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

cdonner
More a well known/popular overall http utils library, hence I'll say both I guess to your question
Greg
the overview words suggests its a "AsyncFileUpload is an ASP.NET AJAX Control" - so in this case I'm guessing it isnt' really a standalone library that could be used in say WinForms development then?
Greg
Winforms? I think you mean asp.net? You mentioned HTTP and httpwebrequest in the title?The AjaxToolkit works with .Net, so in that regard it is not "stand-alone". But you don't need anything else to use it.
cdonner
sorry if I wasn't clear - I'm working with a WinForms application that needs to call via HTTP back end web apps - so this is why I was after a standalone library (i.e. not controls for ASP.net) - may not have used the best terminology
Greg
Ok, my bad. This does not apply then. Some of the other recommendations here should work for you, though.
cdonner
+1  A: 

Do check out HTML Agility Pack on CodePlex :

There's also thread here on SO that may be of interest to you.

BillW
Re htlm agility pack it seems to be more focused around HTML and DOM operations based on a quick skim, as opposed to http utils?
Greg
+4  A: 

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?

Henk Holterman
Normal form post parameters
Greg
+7  A: 

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.

dtb
Umm...how about the specific case I have where I need to do a file upload, but within the same POST I need to include some form parameters? Can WebClient handle this? Thanks
Greg
*multipart/form-data* is your friend.
dtb
Thanks - This is great to now know. I guess going back to my question whether there is a popular library that encapsulated http functions like this? Eg in this case you would just pass files names and key value pairs...
Greg
PS. Here's a link I found that gets close to what I was thinking of ( http://aspnetupload.com/Download-Source.aspx ). 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.
Greg
PSS. Here's the link re how to make use of the helper: http://aspnetupload.com/Upload-File-POST-HttpWebRequest-WebClient-RFC-1867.aspx
Greg
A: 

this is best answer I could determine so far:

Here's a link I found that gets close to what I was thinking of ( aspnetupload.com/Download-Source.aspx ). 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.

Greg
A: 

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";
}
monkey_boys