C# Web Client clear all data - c#

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.

Related

Is there any way to hide/override real IP while using anonymous or transparent proxy?

Is there any way to hide/override real IP while using anonymous or transparent proxy?
I want to send empty in HTTP_X_FORWARDED_FOR. I am using C# Winform. Below is the code snippet.
WebClient wc = new WebClient();
//wc.Headers["HTTP_X_FORWARDED_FOR"] = "0.0.0.0"; --Not working
wc.Proxy = new WebProxy(ipproxy, port);
string t = wc.DownloadString("https://www.leaky.org/ip_tester.pl");
The goal is to send completely anonymous request. The proxies I am using is not brought from any website, they are collected from random sites. It would be great if someone also mention any good site of working proxies.
Thanks
Yes. You can use ProxySharp to generate/get proxy servers to use for your request.
https://github.com/m-henderson/ProxySharp
After you install the ProxySharp nuget package, you can get a random anonymous proxy to use with your request. Like this:
WebClient wc = new WebClient();
var proxyServer = Proxy.GetSingleProxy();
wc.Proxy = new WebProxy(proxyServer);
string t = wc.DownloadString("https://www.leaky.org/ip_tester.pl");
It also has different methods for getting, renewing, and popping proxies from the queue that it generates.
Yes. I built ProxySharp to do exactly what you are trying to do.
ProxySharp has a method that will get you a random anonymous proxy to use with your web request. Check out the repo for more info https://github.com/m-henderson/ProxySharp
The X-Forwarded-For header is generally added by the proxy server, not the client. If you put something there, the proxy server will overwrite or append to it.

Post fields and a file using the new System.Net.WebClient

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.

WebClient restful Delete

I have a simple Restful service being called from a console app so am using WebClient. I am wondering if this call for Delete is correct.
The url looks like localhost/RestService1/Person/1
using (var client = new WebClient())
{
client.UploadString(url, "DELETE", "");
}
I don't like that UploadString does not have an overload without a data parameter. The passing of an empty parameter is not sitting well with me. Is there a better method to use for a DELETE?
I could use WebRequest but I want to just use WebClient to keep it consistent.
Here is the WebRequest block
var request = WebRequest.Create(url);
request.Method = "DELETE";
var response = (HttpWebResponse)request.GetResponse();
Both blocks work fine but what is best? Or is there a better way?
The following works for me:
client.UploadValues(url, "DELETE", new NameValueCollection());
The WebClient class doesn't really lend well to restful api consumption, I've used 3rd party libraries like RestSharp in the past that are geared more towards this type of web request. I'm pretty sure RestSharp just uses HttpWebRequest under the covers, but it provides a lot of semantics that make consuming and reusing rest resources easier.
Go get the Microsoft.Net.Http client libraries http://nuget.org/packages/Microsoft.Net.Http
HttpClient is a much better client to use for working with an API.
Sorry this is my solution in vb.net i sure that anyone can translate to c#
It's very important to drop headers, i had to comment header about Accept and Content-Type and work fine..... of course I did send the token
Dim rest As WebClient = New WebClient()
rest.Headers.Add(HttpRequestHeader.Authorization, "Bearer " & Token)
'rest.Headers.Add(HttpRequestHeader.Accept, "application/json")
'rest.Headers.Add(HttpRequestHeader.ContentType, "application/json")
result = System.Text.Encoding.UTF8.GetString(rest.UploadValues(host_api & uri, "DELETE", New NameValueCollection()))

HTTP GET request and XML answer

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.

WebClient.UploadData correct usage for post request

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

Categories