Webclient does not understand blob:http uri - c#

I am trying to download data from a blob:http link problem is the webclient complains about the url formating. blob:http://localhost/7420f6fc-9c83-43a3-aa53-4a68ebec9518 this format is not know to webclient is there another way to download this data without using Azure calls?
using (var client = new WebClient())
{
//NotSupportedException: The URI prefix is not recognized.
var model = client.DownloadData(new Uri("blob:http://localhost/7420f6fc-9c83-43a3-aa53-4a68ebec9518"));
//Also tried
var model = client.DownloadData("blob:http://localhost/7420f6fc-9c83-43a3-aa53-4a68ebec9518");
}

Related

Upload raw byte data with dotnet core HttpClient

I am updating some old C# code to use HttpClient instead of WebClient. As part of this, I need to upload a byte array of a file to an api.
With WebClient, this worked perfectly fine
byte[] data = GetMyData();
using (var client = new WebClient())
{
//set url, headers etc
var r = client.UploadData(url, "PUT", data);
}
With HttpClient, I've tried various methods, such as
byte[] data = GetMyData();
using (var client = new HttpClient())
{
//set url, headers etc
var r = await client.PutAsync(url, new ByteArrayContent(data));
}
I've also tried different ways of using Multipart data that I found Googling around, but the server does not accept anything I've tried. I don't have a lot of documentation on the server API, I only know that the WebClient way has worked well for many years. Is there a way to recreate the WebClient.UploadData behavior with HttpClient?
Thanks to the commenters for putting me on the right track. The Content-Type headers were not being set correctly for the HttpClient way, by putting it on the actual content. code below.
byte[] data = GetMyData();
using (var client = new HttpClient())
{
//set url, headers etc
var content = new ByteArrayContent(data);
content.Headers.ContentType = new MediaTypeWithQualityHeaderValue(contentType);
var r = await client.PutAsync(url, content);
}

How to handle C# .NET POST and GET commands

The current project I am working on requires a 2 way communication from the bot to my website.
Supposing the example URL is www.example.com/foobar.php or something, can you explain me how to POST and GET data from there?
Thanks a lot.
P.S. - Using webclient right?
I'd suggest using RestSharp. It's a lot easier than using WebClient, and gives you a lot more options:
var client = new RestClient("http://www.example.com/");
//to POST data:
var postRequest = new RestRequest("foo.php", Method.POST);
postRequest.AddParameter("name", "value");
var postResponse = client.Execute(postRequest);
//postResponse.Content will contain the raw response from the server
//To GET data
var getRequest = new RestRequest("foo.php", Method.GET);
getRequest.AddParameter("name", "value");
var getResponse = client.Execute(getRequest);
Yes, you can use WebClient:
using (WebClient client = new WebClient())
{
NameValueCollection nvc = new NameValueCollection()
{
{ "foo", "bar"}
};
byte[] responseBytes = client.UploadValues("http://www.example.com/foobar.php", nvc);
string response = System.Text.Encoding.ASCII.GetString(responseBytes);
}
You can use WebClient
Look up method UploadString and DownloadString

Uri ignore special characters

I got a string that represent a url and i want to download the content via c#
my url contains right to left mark and left to right mark %E2%80%8F & %E2%80%8E.
when i paste the url in the browser i can show the file.
when i use the code in .net i get an error ( because .net ignore those marks and doesnt send them in the request.
this is the query string i got
/MBA%20%281%29/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%A2%D7%A1%D7%A7%D7%99%D7%AA%20%D7%AA%D7%97%D7%A8%D7%95%D7%AA%D7%99%D7%AA%20%28%E2%80%8F%EF%BB%BF13015%E2%80%8E%29%E2%80%8F/%D7%93%D7%A4%D7%99%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA%20%D7%9C%D7%9E%D7%91%D7%97%D7%9F/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%93%D7%A3%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA-2%20%D7%A2%D7%9E-%203%20%D7%A2%D7%9E%D7%95%D7%93%D7%95%D7%AA%20%D7%9C%D7%A8%D7%95%D7%97%D7%91.pdf
using fiddle i can that .net send
/MBA%20(1)/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%A2%D7%A1%D7%A7%D7%99%D7%AA%20%D7%AA%D7%97%D7%A8%D7%95%D7%AA%D7%99%D7%AA%20(%EF%BB%BF13015)/%D7%93%D7%A4%D7%99%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA%20%D7%9C%D7%9E%D7%91%D7%97%D7%9F/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%93%D7%A3%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA-2%20%D7%A2%D7%9E-%203%20%D7%A2%D7%9E%D7%95%D7%93%D7%95%D7%AA%20%D7%9C%D7%A8%D7%95%D7%97%D7%91.pdf
youll notice that marks are gone.
any idea how to solve it?
here some code:
var s = "https://dl.dropboxusercontent.com/1/view/4fsaouo0ob52xkz/MBA%20%281%29/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%A2%D7%A1%D7%A7%D7%99%D7%AA%20%D7%AA%D7%97%D7%A8%D7%95%D7%AA%D7%99%D7%AA%20%28%E2%80%8F%EF%BB%BF13015%E2%80%8E%29%E2%80%8F/%D7%93%D7%A4%D7%99%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA%20%D7%9C%D7%9E%D7%91%D7%97%D7%9F/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%93%D7%A3%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA-2%20%D7%A2%D7%9E-%203%20%D7%A2%D7%9E%D7%95%D7%93%D7%95%D7%AA%20%D7%9C%D7%A8%D7%95%D7%97%D7%91.pdf";
using (HttpClient client = new HttpClient())
{
using (var sr = client.GetAsync(s).Result)
{
Console.WriteLine(sr.Headers);
}
}
I tried to create the uri manually and pass it to the httpClient - save result.
The sr - got 403 error code instead of 200( the link i provided in invalid but the end result is the same).
Try this:
var uri = "https://dl.dropboxusercontent.com/1/view/4fsaouo0ob52xkz/MBA%20%281%29/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%A2%D7%A1%D7%A7%D7%99%D7%AA%20%D7%AA%D7%97%D7%A8%D7%95%D7%AA%D7%99%D7%AA%20%28%E2%80%8F%EF%BB%BF13015%E2%80%8E%29%E2%80%8F/%D7%93%D7%A4%D7%99%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA%20%D7%9C%D7%9E%D7%91%D7%97%D7%9F/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%93%D7%A3%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA-2%20%D7%A2%D7%9E-%203%20%D7%A2%D7%9E%D7%95%D7%93%D7%95%D7%AA%20%D7%9C%D7%A8%D7%95%D7%97%D7%91.pdf";
string Url= System.Web.HttpUtility.UrlDecode(uri);
//objDataToXml.GenerateXml();
using (HttpClient client = new HttpClient())
{
using (var sr = client.GetAsync(Url).Result)
{
Console.WriteLine(sr.Headers);
}
}

webclient or httpwebrequest to retrieve hrefs and url

How do I use either webclient or httpwebrequest to do two things:
1)Say after downloading the resource as a string using:
var result = x.DownloadString("http://randomsite.com);
there's a relative url(also query string):
Click here to get your name and age
how do I click(follow) on that link using webclient? after initially loading the resource in result. i was able to use htmlagilitypack to isolate the href but I would now like to follow it in code.
2) If the httpwebrequest does not redirect but instead loads the same page with different parameters how would i use webclient to retrieve the new url that is generated?
i.e if i call
var result = x.DownloadString("http://randomsite.com);
but this actually calls
http://randomsite.com/q?site=default
I then want to retrieve the second url
Thanks in advance
You can construct the url from the link and the link that you just downloaded like this:
Uri baseUri = new Uri("http://randomsite.com");
Uri myUri = new Uri(baseUri, "/q?name=john&age=50");
Console.WriteLine(myUri.ToString()); // gives you http://randomsite.com/q?name=john&age=50
This also works if you base Url has url parameters.
As for the second question, i guess you meant that the request was redirected and you want that url instead? Then the easiest way to do so is to sub-class WebClient described here.
Uri baseUri = new Uri("http://randomsite.com");
using(var client=new WebClient())
{
var result = client.DownloadString(myUri);
//get href via HtmlAgilityPack...
Uri myUri = new Uri(baseUri, "/q?name=john&age=50");
result = client.DownloadString(myUri);
}

WebUrl Download

I am using c#.
I am trying to see if a web url Contains a string.
I am currently using
using (var client = new WebClient())
{
result = client.DownloadString(WebUrl);
}
if (result.Contains(String))
{
return true;
}
I am doing this for about 300 Urls, is there a faster way or do I have to download each WebPage?

Categories

Resources