1

i'm trying to invoke a webapi with the new System.Net.WebClient and didnot found any special examples.

The goal is simulate a traditional form post with some fields and a file.

how can i do it using the System.Net.WebClient or where can i find some examples?

thanks in advance

Flavio CF Oliveira
  • 5,235
  • 13
  • 40
  • 63

2 Answers2

1

I think you need this:

http://dzimchuk.net/post/uploading-a-file-over-http-in-net

This is a very well written blog. See the last example.

0

There are a lot of examples if you do a fast google search, anyway, here goes some samples:

Simple GET

WebClient webClient = new WebClient();
webClient.Headers["Accept"] = "application/json"; //setting headers

string json = webClient.DownloadString(url);

Simple POST

NameValueCollection values = new NameValueCollection();
values["user"] = username;
values["pwd"] = password;
webClient.UploadValues(url, values);

There's also an UploadData that sends byte arrays and UploadFile that allows you to upload files directly from disk.

Eric Lemes
  • 561
  • 3
  • 10
  • my question is not about the simple get or simple post, is about do a post like a multipart/form-data form, where is possible to send in the same request, a file and a couple of KeyValue fields – Flavio CF Oliveira Jul 14 '14 at 17:51
  • I didn't find a suitable implementation in WebClient. Probably it's only possible through HttpWebRequest. Unfortunately the WebClient isn't a complete implementation. I had troubles with some other issue like using cookies, for example. – Eric Lemes Jul 14 '14 at 18:05
  • I'd make an extension method for that. – Eric Lemes Jul 14 '14 at 18:05