Uri ignore special characters - c#

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

Related

Webclient does not understand blob:http uri

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

C# How to put curly brackets in URL

I've been trying to get the data that I need from Facebook's graph api explorer but unfortunately, cant pass the fields necessary in URL via C#.
Here's what I've tried so far
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://graph.facebook.com");
HttpResponseMessage response = client.GetAsync($"cocacola?fields=posts{id,created_time,permalink_url,message,link,type,full_picture}&access_token={textBox1.Text}").Result;
response.EnsureSuccessStatusCode();
string result = response.Content.ReadAsStringAsync().Result;
var jsonRes = JsonConvert.DeserializeObject<dynamic>(result);
var returned = jsonRes.ToString();
MessageBox.Show(returned);
}
What's needed to be done here for fetching data via API? :)
As it turns out, there is an issue because you're using string interpolation and also want curly braces in your string. You'll have to escape them by doubling them.
HttpResponseMessage response = client.GetAsync($"cocacola?fields=posts{{id,created_time,permalink_url,message,link,type,full_picture}}&access_token={textBox1.Text}").Result;
Your approach is incorrect Because Those Variables You Are Trying To Add In Url Are Actually Getting On String, You must escape them to get their valueTherefore Change
HttpResponseMessage response = client.GetAsync($"cocacola?fields=posts{id,created_time,permalink_url,message,link,type,full_picture}&access_token={textBox1.Text}").Result;
To
HttpResponseMessage response = client.GetAsync($"cocacola?fields="+posts{id,created_time,permalink_url,message,link,type,full_picture}+"&access_token="+{textBox1.Text}).Result;
But here to its incorrect cause i dont think {textBox1.Text} and posts{id,created_time,permalink_url,message,link,type,full_picture} Means Anything

How do I get the destination URL of a shortened URL?

I have an API (https://www.readability.com/developers/api/parser#idm386426118064) to extract the contents of the webapges, but on passing a shortened url or an url that redirects to other, it gives error.
I am developing windows phone 8.1 (xaml) app. Is there any way to get the destination url in c# or any work around?
eg url - http://www.bing.com/r/2/BB7Q4J4?a=1&m=EN-IN
You could intercept the Location header value before the HttpClient follows it like this:
using (var handler = new HttpClientHandler())
{
handler.AllowAutoRedirect = false;
using (var client = new HttpClient(handler))
{
var response = await client.GetAsync("shortUrl");
var longUrl = response.Headers.Location.ToString();
}
}
This solution will always be the most efficient because it only issue one request.
It is possible however, that the short url will reference another short url and consequently cause this method to fail.
An alternative solution would be to allow the HttpClient to follow the Location header value and observe the destination:
using (var client = new HttpClient())
{
var response = client.GetAsync("shortUrl").Result;
var longUrl = response.RequestMessage.RequestUri;
}
This method is both terser and more reliable than the first.
The drawback is that this code will issue two requests instead of one.
You can get the ResponseUri from GetResponse():
string redirectedURL = WebRequest.Create("http://www.bing.com/r/2/BB7Q4J4?a=1&m=EN-IN")
.GetResponse()
.ResponseUri
.ToString();
Interesting article, by the way.
You need to inspect the headers returned from the URL.
If you get HTTP return codes 301 or 302, then you are being notified that the page is redirecting you to another URL.
See http://www.w3.org/Protocols/HTTP/HTRESP.html for more details about HTTP return codes.

How to connect another server using network?

I want to connect to another server using network.
So I write below code.
var webRequest = WebRequest.Create(#"10.3.4.56");
using (var response = webRequest.GetResponse())
{
using (var rd = new StreamReader(response.GetResponseStream()))
{
var soapResult = rd.ReadToEnd();
}
}
But there is error and it says
Invalid URI: The format of the URI could not be determined.
How to solve it?
The string you pass in to WebRequest.Create needs to be a valid Uri. Try WebRequest.Create("http://10.3.4.56").
TIP! Use the static Uri.IsWellFormedUriString method to check if the URI string is valid.
try including the shema
"http://10.3.4.56"

WebRequest using Mozilla Firefox

I need to have access at the HTML of a Facebook page, to extract from it some data. So, I need to create a WebRequest.
Example:
My code worked well for other sites, but for Facebook, I must be logged in to can access the HTML.
How can I use Firefox data for creating a WebRequest for Facebook page?
I tried this:
List<string> HTML_code = new List<string>();
WebRequest request = WebRequest.Create(URL);
using (WebResponse response = request.GetResponse())
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
string line;
while ((line = stream.ReadLine()) != null)
{
HTML_code.Add(line);
}
}
...but the HTML resulted is the HTML of Facebook Home Page when I am not logged in.
If what you are trying to is retrieve the number of likes from a Facebook page, you can use Facebook's Graph API service. Just too keep it simple, this is what I basically did in the code:
Retrieve the Facebook page's data. In this case I used the Coke page's data since it was an example FB had listed.
Parse the returned Json using Json.Net. There are other ways to do this, but this just keeps it simple, and you can get Json.Net over at Codeplex. The documentation that I looked for my code was from this page in the docs. Their documentation will also help you with parsing and serializing even more Json if you need to.
Then that basically translates in to this code. Just note that I left out all the fancy exception handling to keep it simple as using networking is not always reliable! Also don't forget to include the Json.Net library in your project!
Usings:
using System.IO;
using System.Net;
using Newtonsoft.Json.Linq;
Code:
string url = "https://graph.facebook.com/cocacola";
WebClient client = new WebClient();
string jsonData = string.Empty;
// Load the Facebook page info
Console.WriteLine("Connecting to Facebook...");
using (Stream data = client.OpenRead(url))
{
using (StreamReader reader = new StreamReader(data))
{
jsonData = reader.ReadToEnd();
}
}
// Get number of likes from Json data
JObject jsonParsed = JObject.Parse(jsonData);
int likes = (int)jsonParsed.SelectToken("likes");
// Write out the result
Console.WriteLine("Number of Likes: " + likes);

Categories

Resources