C# - How to send search keywords like google [duplicate] - c#

This question already has answers here:
Encoding parameters for a URL
(3 answers)
Closed 4 years ago.
I want to send search keywords with QueryString like google, atc...
and i need a function which convert text like this
Metallica nimes concert 2009 :)
to this:
Metallica+nimes+concert+2009+%3A%29
and retrieve it
Thanks and advance

you need to encode the string using:
HttpUtility.UrlEncode("Metallica nimes concert 2009 :)")
you'll need to reference to System.Web
This will work on simple strings, if you want to encode more characters see:
Server.UrlEncode vs. HttpUtility.UrlEncode. as Panagiotis Kanavos recommended

Trying to encode a string into a URL?
Why not use system.net.webutility.urlencode
Docs here
Rock here

Related

Is there a native way to parse key/value pairs into an array? [duplicate]

This question already has answers here:
Get URL parameters from a string in .NET
(17 answers)
Closed 7 years ago.
I have a string that is the contents of a webpage in this format :
value=foo&value1=bar&value2=foobar
What I would like to know is, is there a built-in way to convert this into an object/list of object/whatever so that I can loop through or access the values by keyname
I know I can split on '&', and then split on '=', but I believe there is a built-in way to do this, I just do not know what it is.
The language I am working in is C#.
You can use HttpUtility.ParseQueryString.
Eg.
NameValueCollection values = HttpUtility.ParseQueryString("value=foo&value1=bar&value2=foobar");

Equivalent to Javascript's encodeURI? [duplicate]

This question already has answers here:
Does C# have an equivalent to JavaScript's encodeURIComponent()?
(7 answers)
UrlEncode - Javascript vs. C# [duplicate]
(5 answers)
Closed 9 years ago.
C#'s equivalent to encodeURIComponent is well-covered on SO and elsewhere, but what about encodeURI? Basically I want to encode invalid URL characters only and not reserved characters such as /, :, etc. So
"http://www.example.com/my cool page"
would be encoded to
"http://www.example.com/my%20cool%20page"
Is there something baked into .NET to do this? Or is a regex my best bet?
Try
Uri.EscapeUriString("http://www.mysite.com/my cool page")
Try
Server.URLEncode(uri.ToString)
Try this:
HttpUtility.UrlEncode(String)
For example:
var url_encoded_string = HttpUtility.UrlEncode(userInput);
You could use System.Net.WebUtility.UrlEncode(string value).

Easiest way to extract some html from string [duplicate]

This question already has answers here:
What is the best way to parse html in C#? [closed]
(15 answers)
Closed 9 years ago.
I have a long c# string of HTML code and I want to specifically extract bullet points "<ul><li></li></ul>".
Say I have the following HTML string.
var html = "<div class=ClassC441AA82DA8C5C23878D8>Here is a text that should be ignored.</div>This text should be ignored too<br><ul><li>* Need this one</li><li>Another bullet point I need</li><li>A bulletpoint again that I want</li><li>And this is the last bullet I want</li></ul><div>Ignore this line and text</div><p>Ignore this as well.</p>Text not important."
I need everything between the '<ul>' to '</ul>' tags. The '<ul>' tag can be excluded.
Now regular expression is not my strongest side, but if that can be used I need some help.
My code is in c#.
You should use the HtmlAgilityPack for things like this. I wrote a little introduction to it a while ago that may help you get going: http://colinmackay.scot/2011/03/22/a-quick-intro-to-the-html-agility-pack/

include value with '&' in querystring [duplicate]

This question already has answers here:
How to build a query string for a URL in C#?
(39 answers)
Closed 9 years ago.
I have to write this data in querystring:
http://localhost:1256/4.market.ph.local/WEP/Add.cshtml?data=me+&+you
I got an error because of that symbol '&' i used.
In c# you can use this:-
HttpUtility.UrlEncode("http://localhost:1256/4.market.ph.local/WEP/Add.cshtml?data=me+&+you");
HttpUtility is a part of System.Web and this will ensure an of the non permitted query string char are url Encoded.
Once you do this you will get something like this http%3a%2f%2flocalhost%3a1256%2f4.market.ph.local%2fWEP%2fAdd.cshtml%3fdata%3dme%2b%26%2byou
On the receiver just decode it back.
Use urlencode($yourstring) or if you are hard coding it, use %26 to represent the ampersand.

Get text from HTML [duplicate]

This question already has answers here:
How do you convert Html to plain text?
(20 answers)
Closed 1 year ago.
I need a way to get all text from my aspx files.
They may contain javascrip also but I only need this for the HTML code.
Basically I need to extract everything on Text or Value attributes, text within code, whatever...
Is there any parser API available?
Cheers!
Alex
As an alternative, you might consider playing with Linq to XML to strip the interesting stuff out.

Categories

Resources