How do you use an Ampersand in an HTTPCookie in VB.NET? - vb.net

I have a cookie saved to the user as follows...
Dim searchCookie As HttpCookie = New HttpCookie("SearchCriteria")
searchCookie.Item("SearchText") = FullSearchCriteria.SearchText
searchCookie.Item("SearchType") = FullSearchCriteria.SearchType
The SearchText stores a value they have input in a previous page. We have observed if there is an ampersand in the cookie (eg Tyne & Wear), then the cookie doesn't save subsequent values (SearchType).
What happens is the cookie is output like this:
SearchText=Tyne &
Obviously the ampersand is confusing the cookie. Is there a way to prevent this happening?

You can use the URLEncode method.
Something like:
imports HttpContext.Current
...
Dim searchCookie As HttpCookie = New HttpCookie("SearchCriteria")
searchCookie.Item("SearchText") = Server.UrlEncode(FullSearchCriteria.SearchText)
searchCookie.Item("SearchType") = Server.UrlEncode(FullSearchCriteria.SearchType)
This is essential as only certain characters are allowed in cookies with characters such as ampersands breaking them.

D'oh! I'm such a dork...
Dim searchCookie As HttpCookie = New HttpCookie("SearchCriteria")
searchCookie.Item("SearchText") = HttpContext.Current.Server.UrlEncode(FullSearchCriteria.SearchText)
searchCookie.Item("SearchType") = HttpContext.Current.Server.UrlEncode(FullSearchCriteria.SearchType)

The cookie values need to be encoded. I'm no VB expert, but it looks like this is done with the method
System.Web.HttpUtility.UrlEncode

Related

WebClient is not sending the form parameters correctly?

If I use Nightingale REST Client to download a CSV file it works, but if I try to duplicate the configuration and get the file using VB.Net it fails.
Here is a screencap of my Nightingale config and a successful response:
This is my code to try and do the same thing:
Dim values As NameValueCollection
Dim result As Byte()
Using client = New WebClient()
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
values = New NameValueCollection From {{"_EVENTARGUMENT", "CSV,Export,,M"}, {"_EVENTTARGET", "dnn$ctr520$dnn"}, {"txtFlt_AI", ""}, {"txtFlt_CoName", ""}, {"cboFlt_ParishName", ""}, {"cboFlt_Fyear", ""}, {"ScrollTop", ""}}
result = client.UploadValues("https://internet.deq.louisiana.gov/portal/DIVISIONS/UNDER-GROUND-STORAGE-TANK/CURRENT-UST-TANK-CERTIFICATES", "POST", values)
End Using
Instead of the CSV file, I get the web page that this URL normally retrieves. What am I missing here? Thanks.
hard to tell from just this info, i'd say make sure you have all your headers input, and you might have to set the user agent, and perhaps along with it, the referrer.
also look into your auth, if you receive any sort of token or cookie, you'll need to store and pass that along too.

Get Sitefinitys Generated Password Reset URL

I am trying to get the URL from the password reset which I receive via email of the sitefinity frontend login.
I need to send the URL with the username which is enterd in the form to a server to send the email.
I already tried to override the SendResetPasswordEmail of the LoginFormModel but that only gives me the URL where the reset is located at. (localhost/login/resetpassword)
It looks like the URL is generated in the method SendRecoveryPasswordMail of the Telerik.Sitefinity.Security.UserManager which is not overridable.
Is there a way to get the generated recovery URL to use it in a custom method?
Thanks in advance
Since you already have the URL of the reset password page, I guess your issue is getting the proper query string to pass to that page.
Looking at the source code with JustDecompile, the query string is made up of this:
?vk=userValidationKeyEncoded&cp=pr
The cp=pr seems to be hardcoded, so we leave it as is, the question is how the userValidationKeyEncoded is made.
Again, looking in the code, it is this line:
string userValidationKeyEncoded = UserManager.GetUserValidationKeyEncoded(userByEmail);
And finally:
private static string GetUserValidationKeyEncoded(User user)
{
object[] providerName = new object[] { user.ProviderName, ',', user.Id, ',', DateTime.UtcNow };
string str = string.Format("{0}{1}{2}{3}{4}", providerName);
return SecurityManager.EncryptData(str).UrlEncode();
}
You can use the above code to manually generate the validationKey.

Trying to add all cookies together from a webrequest, but when I print it, it sends back system.net.cookiecontainer

I am struggling getting a cookie, from a website, when I alert the cookie,it just returns: system.net.cookiecontainer Here's how I am trying to get the cookie:
'get the cookie for the post request !important
Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://www.dailymail.co.uk/home/index.html"), HttpWebRequest)
req.Method = "GET"
'iniate the cookie container for the post request
Dim tmpcookie As New CookieContainer
'get the cookie.
Dim postcookie = DirectCast(req.GetResponse(), HttpWebResponse)
tmpcookie.Add(postcookie.Cookies)
'assign the cookie to use outsie the scope (background worker)
textcookie = tmpcookie.ToString()
but when I alert textcookie I get what I said above :(
tmpcookie is a CookieContainer. You're calling ToString on a CookieContainer, it does what it's specified to do: output the fully qualified type name, "System.Net.CookieContainer".
It's like doing (New List(Of Object)).ToString() - it's going to output "System.Collection.Generics.List", not a string representing every item in that list.
You'll want to iterate the cookies in that container, and concatenate/build (?) a string from each individual cookie in that container.

How to append link in current URL in Selenium using java

I want to know how to append link in current URL. For eg: me link is https://www.google.co.in/ in current program now I have to append /#q=ask+questions in this URL.
Please help.
I know how to get current url(by using getCurrentUrl() syntax)
Thanks
You could use URIBuilder. Something like this:
String someUrl = "https://www.google.co.in";
// or perhaps
// String someUrl = browser.getCurrentUrl();
URIBuilder uri = new URIBuilder(someUrl);
uri.setPath("search");
uri.addQueryParam("q", "ask+questions");
Assert.assertEquals(uri.toString(), "https://www.google.co.in/search?q=ask%2Bquestions");
// or perhaps
// browser.get(uri.toString());
getCurrentUrlUrl() has a return type of String. So you can save it's value and play around like you can with any String.
Take an example, I want to get this url and then append your string and then get() this new webpage:
String url = driver.getCurrentUrl();
String newurl = url+"/#q=ask+questions";
driver.get(newurl);

Special character into querystring .NET

I need to send the follow querystring:
http://prod.intranet.siemens.com.br/drvs/index.aspx?page=2&pag=4&varpatch=%20C:\Documents%20and%20Settings\OPE253\My%20Documents\Ca$##!
Then i try to assing this to a string,but .NET break string at
http://prod.intranet.siemens.com.br/drvs/index.aspx?page=2&pag=4&varpatch=%20C:\Documents%20and%20Settings\OPE253\My%20Documents\Ca$#
'#" do not appears in querystring
Any ideas?
No, because "#" is a reserved character. It's used to link to a specific location in a web page:
http://en.wikipedia.org/wiki/HTML_anchor#Overview
So browsers split the URL at the "#".
You'll need to encode the "#" as "%23"
You need to use String.Replace:
Dim outputURL As String = inputURL.Replace("#", "%23")
or HttpUtility.UrlEncode (only encode the querystring):
Dim outputQueryString As String = HttpUtility.UrlEncode(inputQueryString)