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
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.
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.
Related
I have website which gives me different value (example: value="c04f8d84708f9865f4e04802c51c2f90") every time when I clear cookies in my standard browsers (Firefox, Chrome...).
But when I use client I get always same value, my code:
WebClient client1 = new WebClient();
client1.Headers.Add("Cache-Control", "no-cache");
client1.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
string html = client1.DownloadString("website");
client1.Dispose();
How I can clear all data/cookies so I can get different value ?
Have you tried this:
httpClient.DefaultRequestHeaders.Accept.Clear();
use HttpRequest object instead of WebClient, and hopefully (if you place your doubts in WebClient) everything will be solved. If it wasn't solved with HttpRequest, then the problem really IS somewhere else.
Hope this helps.
i'm having a little piece of python code which makes a web request using the urllib2 as you can se below
import json
import urllib2
urlRequest = urllib2.Request('<link>')
urlRequest.add_header('Content-Type', 'application/json')
urlRequest.add_header('RegistrationToken', '<token>')
data = {
'content': '<c>',
'messagetype': 'RichText',
'contenttype': 'text',
'id': '<id>'
}
urllib2.urlopen(urlRequest, json.dumps(data))
As i was trying to do it in C# i came across the following problems
how to i send the data
how do i add the headers?
After googling for a while i managed to write this code:
var request = (HttpWebRequest)WebRequest.Create(url_input.Text);
request.ContentType = "application/json";
request.Headers["RegistrationToken"] = rtoken_input.Text;
request.GetResponse();
I managed to deal with the headers part but the question on the data still remains. Also what is the best way to json encode something?
Anyone who knows what to do?
If you are after serializing the POST data to a JSON payload there are few options.
1) System.Web.Helpers.Json.Encode MSDN Link
2) using the JSON.NET library Link
As for your attempt on converting python to C# you are on the correct track.
Refer to this link
Alternatively you could make use of the WebClient class MSDN Link
Refer to this link as well
Pseudo code
var client = new WebClient();
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("RegistrationToken", "<token>");
string response = client.UploadString("<link>", "<json string>");
I am new to C#, I need to send HTTP GET request and read answer. I am familiar with Java and easy can do it URLConnection class but I don't know in c#. Can anybody help ?
The simplest way is to use WebClient:
WebClient client = new WebClient();
string text = client.DownloadString(url);
(That's the synchronous form; it also supports asynchronous requests.)
For more control you might want to use HttpWebRequest.
i think i am going a bit crazy, when i test this on my local webserver, it works fine
when i go out to the live website, it returns a blank string instead of the data i am expecting
i am not that familiar with C#, so i just wanted to check i am doing things right.
the data is just plain ascii text
wc = new WebClient();
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
response = wc.UploadData(this.urlUpdate, Encoding.ASCII.GetBytes("data=" + HttpUtility.UrlEncode(buf.ToString())));
s = Encoding.ASCII.GetString(response);
It really depends what you are trying to do... I'm not sure, for example, why you are url-encoding data in the body. An easier way to post key/value pairs is with UploadValues;
NameValueCollection inputs = new NameValueCollection();
string value = ...
inputs.Add("data", value);
webClient.UploadValues(address, inputs);
Trying to use RSS.NET. Example on the site is: (C#)
string url = "http://sourceforge.net/export/rss2_sfnews.php?feed";
RssFeed feed = RssFeed.Read(url);
RssChannel channel = (RssChannel)feed.Channels[0];
listBox.DataSource = channel.Items;
However, this fails because I need to access the feed through a proxy. How do I do this?
An overload for RssFeed.Read() takes HttpWebRequest. I think this may be the way of setting this up, but I haven't used this before. Help! :)
You can indeed use the HttpWebRequest overload of the RssFeed.Read() function. The following should work
string url = "http://sourceforge.net/export/rss2_sfnews.php?feed";
string proxyUrl = "http://proxy.example.com:80/";
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
WebProxy proxy = new WebProxy(proxyUrl,true);
webReq.Proxy = proxy;
RssFeed feed = RssFeed.Read(webReq);
If you need a username and password for the proxy there is a more detailed example here.
There is an overload of the RssFeed.Read() method that accepts an HttpWebRequest. You can set the proxy on the HttpWebRequest and read it that way. This sets the proxy for the RSS feed in particular. You would need to do this for each feed you read in.
You can set the default proxy using System.Net.WebRequest.DefaultWebProxy, prior to calling RssFeed.Read(String). Do this just once; it will apply to all Rss feeds you read, as well as all other http communicatious going out from your app.