How can i get website text wothout using web browser? - c#

I tryed to do a webbrowser that fro, him i get the text.
But insted of getting the text is downloading to my computer the file.
How can i get this text without using it?
Thanks

You can use a WebClient:
string output = string.Empty;
using (WebClient wc = new WebClient())
{
output = wc.DownloadString("http://stackoverflow.com");
}

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

How can i search a hebrew word from a website using c#

Im trying to search a Hebrew word in a website using c# but i cant figure it out.
this is my current state code that im trying to work with:
var client = new WebClient();
Encoding encoding = Encoding.GetEncoding(1255);
var text = client.DownloadString("http://shchakim.iscool.co.il/default.aspx");
if (text.Contains("ביטול"))
{
MessageBox.Show("idk");
}
thanks for any help :)
The problem seems to be that WebClient is not using the right encoding when converting the response into a string, you must set the WebClient.Encoding property to the expected encoding from the server for this conversion to happen correctly.
I inspected the response from the server and it's encoded using utf-8, the updated code below reflects this change:
using (var client = new WebClient())
{
client.Encoding = System.Text.Encoding.UTF8;
var text = client.DownloadString("http://shchakim.iscool.co.il/default.aspx");
// The response from the server doesn't contains the word ביטול, therefore, for demo purposes I changed it for שוחרות which is present in the response.
if (text.Contains("שוחרות"))
{
MessageBox.Show("idk");
}
}
Here you can find more information about the WebClient.Encoding property:
https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.encoding?view=netframework-4.7.2
Hope this helps.

Scrape InnerText from body of website C#

I'm trying to gather the data from this website: http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=f2pshrympy
using HtmlAgilityPack;
using System;
var webGet = new HtmlWeb();
var document = webGet.Load("http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=f2pshrympy");
var bodyText = document.DocumentNode.SelectNodes("/html/body/text()");
Console.WriteLine(bodyText);
Console.ReadLine();
When the program is run nothing is printed to the console and there are no errors.
screenshot of the console
I'm guessing that nothing is being found with the XPath "/html/body/text()", any ideas how I can go around fixing this?
Your page is pure text. So you don't need any tool like HtmlAgilityPack to parse it. Just download it and use it.
using (var wc = new WebClient())
{
var bodyText = wc.DownloadString("http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=f2pshrympy");
}

how to download a file using sync webclient request in c# with out showing popup

I am using "webclient" to download and save a file by url in windows application.
here is my code:
WebClient wc = new WebClient();
wc.Headers.Add(HttpRequestHeader.Cookie, cc);
wc.DownloadFile(new Uri(e.Url.ToString()), targetPath);
this is working fine local system.(downloading the file and saved to target path automatically with out showing any popup).
But when i am trying to execute the .exe in server its showing save/open popup.
Is there any modifications require to download a file in server settings.
Please help me to download the file with out showing popup in server too.
thanks in advance..
Finally i got the solution for this issue..
herw the code:
WebClient wc = new WebClient();
wc.Headers.Add(HttpRequestHeader.Cookie, cc);
using (Stream data = wc.OpenRead(new Uri(e.Url.ToString())))
{
using (Stream targetfile = File.Create(targetPath))
{
data.CopyTo(targetfile);
}
}
here i just replaced the code
wc.DownloadFile(new Uri(e.Url.ToString()), targetPath);
with the blow lines:
using (Stream data = wc.OpenRead(new Uri(e.Url.ToString())))
{
using (Stream targetfile = File.Create(targetPath))
{
data.CopyTo(targetfile);
}
}
Now its working fine..
Thanks all for ur response..

C# and ASP.net saving html into a string or a file

I'm new to ASP and I was wondering if there is a way to save the source of the web-page into a string variable or a .txt file given a website address using C# or ASP.net with C#.
If its possible, example code and information on what libraries to reference would be very helpful.
You can use the WebClient class for that:
To a string variable:
string result;
using (WebClient wc = new WebClient())
result = wc.DownloadString("http://stackoverflow.com");
To a file:
using (WebClient wc = new WebClient())
wc.DownloadFile("http://stackoverflow.com", #"C:\test\test.txt");
Sure thing:
HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse;
string html = new StreamReader(response.GetResponseStream()).ReadToEnd();
At a basic high level.
You should take a look at the WebClient Class
An example can be found on the link posted above.

Categories

Resources