VB.Net Convert text to WWW form - vb.net

I want to convert text into the WWW form.
E.g: # should be %40, % should be %25 etc...
There's fine encoder here, but i want to do it in VB.Net.
I need this for httpwebrequest, i think it has something to do with x-www-form-urlencoded.

You can use the Uri.EscapeDataString() method for that:
Dim OriginalURL As String = "http://www.example.com/some file with spaces.php?q1=plus+&q2=at#&q3=svenska språkets 'ö'"
Dim EncodedURL As String = Uri.EscapeDataString(OriginalURL)
Online test: https://ideone.com/h5fqm1
And if you want to just escape parts of the URL but still keep valid components such as : / = ? & (etc.) you'd use Uri.EscapeUriString().

Related

Validation of dynamic text in testing

I am trying to validate a pin code in my application. I am using Katalon and I have not been able to find an answer.
The pin code that I need to validate is the same length but different each time I run the test and looks like this on my page: PIN Code: 4938475948.
How can I account for the number changing each time I run the test?
I have tried the following regular expressions:
assertEquals(
"PIN Code: [^a-z ]*([.0-9])*\\d",
selenium.getText("//*[#id='RegItemContent0']/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div[1]/div[3]/ul/li[2]/span")
);
Note: This was coded in Selenium and converted to Katalon.
In Katalon, use a combination of WebUI.getText() and WebUI.verifyMatch() to do the same thing.
E.g.
TestObject object = new TestObject().addProperty('xpath', ConditionType.EQUALS, '//*[#id='RegItemContent0']/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div[1]/div[3]/ul/li[2]/span')
def actualText = WebUI.getText(object)
def expectedText = '4938475948'
WebUI.verifyMatch(actualText, expectedText, true)
Use also toInteger() or toString() groovy methods to convert types, if needed.
Editing upper example but to get this working
TestObject object = new TestObject().addProperty('xpath', ConditionType.EQUALS, '//*[#id='RegItemContent0']/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div[1]/div[3]/ul/li[2]/span')
def actualText = WebUI.getText(object)
def expectedText = '4938475948'
WebUI.verifyMatch(actualText, expectedText, true)
This can be done as variable but in Your case I recommend using some java
import java.util.Random;
Random rand = new Random();
int n = rand.nextInt(9000000000) + 1000000000;
// this will also cover the bad PIN (above limit)
I'd tweak your regex just a little since your pin code is the same length each time: you could limit the number of digits that the regex looks for and make sure the following character is white space (i.e. not a digit, or another stray character). Lastly, use the "true" flag to let the WebUI.verifyMatch() know it should expect a regular expression from the second string (the regex must be the second parameter).
def regexExpectedText = "PIN Code: ([0-9]){10}\\s"
TestObject pinCodeTO = new TestObject().addProperty('xpath', ConditionType.EQUALS, '//*[#id='RegItemContent0']/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div[1]/div[3]/ul/li[2]/span')
def actualText = WebUI.getText(pinCodeTO)
WebUI.verifyMatch(actualText, expectedText, true)
Hope that helps!

Getting a vaule in a link for later usage in Selenium

I have a link on my webpage which I need to get the value from and save in for later usage (constructing a direct URL).
The html-link I want to obtain the value from look like this:
<a ng-bind="saving.customerContractName || (saving| savingscontract:$parent.$parent.cmsData) " ng-attr-target="{{(saving.type === 'ASK') ? '_blank' : undefined}}" ng-href="/lpn/mo/Logon.action?avtalenummer=176742" class="ng-binding" target="" href="/lpn/mo/Logon.action?avtalenummer=176742">Fondskonto Link (176742)</a>
The value I need to obtain is 176742.
Any tips on how to extract this value? And further use it in a direct URL call (something) like this:
String url2 = "https://www2-t.storebrand.no/ppjs/#/savings/index/THE_VALUE_HERE";
driver.get(url2);
this might work.
txt = driver.find_element_by_partial_link_text("Fondskonto Link").get_attribute("href").split("=")[1]
url = "https://www2-t.storebrand.no/ppjs/#/savings/index/%s" % txt
driver.get(url)

Get text between quotes in WebBrowser or HttpWebRequest? VB.NET

I have an API, the API returns the input like this:
{"phrase":"decrypted","parsed":"encryptedcode","response":"done","code":6}
Everything around "decrypted" and "encryptedcode" stays the same.
I need to get the "decrypted" part only. No idea where to even begin.
I'm using WebBrowser, since I'm not any good at HttpWebRequest, so if you could answer as WebBrowser code instead of HttpWebRequest, I would appreciate it.
Answer
clean = WebBrowser1.Document.Body.InnerText.Replace("phrase", "").Replace(":", "").Replace("parsed", "").Replace(md5, "").Replace("code", "").Replace("The MD5 hash was cracked.", "").Replace("""", "").Replace("6", "").Replace("}", "").Replace("{", "").Replace(",", "").Replace("response", "")
so I could do
listbox.items.add(clean)
and it wouldn't look messy.
Just Replace : to ,
Then you input looks like
{"phrase", "decrypted", "parsed", "encryptedcode", "response", "done", "code", 6}
then do as below
Dim str() As String = {"phrase", "decrypted", "parsed", "encryptedcode", "response", "done", "code", 6}
Dim decrypted As String = str(1)
you get Decrypted part

HMACSHA1 gives different output between JS and VB.Net

I'm trying to translate a JavaScript application of TOTP to VB.Net: http://blog.tinisles.com/2011/10/google-authenticator-one-time-password-algorithm-in-javascript/
I have encountered a problem during translation of the HMAC-part:
//Javascript:
var hmacObj = new jsSHA("Hello World!", 'HEX');
var hmac = hmacObj.getHMAC("secret", 'HEX', 'SHA-1', "HEX");
This is a codesnippet of my translation in VB.Net
'VB.Net:
Dim hmacObjTest As New HMACSHA1(System.Text.Encoding.UTF8.GetBytes("secret"))
Dim hmacTest As Byte() = hmacObjTest.ComputeHash(System.Text.Encoding.UTF8.GetBytes("Hello World!"))
Dim hmacHexTest As New StringBuilder()
For i As Integer = 0 To hmacTest.Length - 1
hmacHexTest.Append(hmacTest(i).ToString("x2"))
Next i
Dim strTest As String = "HMAC = " & hmacHexTest.ToString()
The problem is that i get different output from the two languages:
Output JS: 5efed98b0787c83f9cb0135ba283c390ca49320e //Tested from jsSha demo: http://caligatio.github.io/jsSHA/
Output VB.Net: 87b0154b8420c0b58869ca103f481e824d8876ea
The outputs are not at all the same like they are in this question: hmacsha1 output hex strings different between vb.net and python
Does anyone know where I might be doing something wrong?
Hashes don't work on strings - they work on the binary representation of the string. Now you use UTF-8 as encoding for the dotnet version, while the JavaScript version is very likely not to use UTF-8 - so you get different binary representations, resulting in different hashes.
Use either webttolkit or the hackish var utfstring = unescape(encodeURIComponent(rawstring)); to convert to UTF-8 before calcualting the hash.

pass post params to WebClient.UploadFileAsync

hello
i have a simple file upload with just one extra post parameter which is imgtitle.
it's used to rename the image after it's been transferred to the upload location on the server.
PHP:
<?php
$imgtitle = $_POST['title'];
$current_image=$_FILES['file']['name'];
$extension = substr(strrchr($current_image, '.'), 1);
$new_image = $imgtitle. "." . $extension;
$destination="uploads/".$new_image;
$action = copy($_FILES['file']['tmp_name'], $destination);
echo "--".$imgtitle."--";
?>
the upload works fine here.
http://tnsatchat.heliohost.org/tnsatchat/upload/upload.html
/upload/uploads/abcd.jpg
what i want to do is make a simple vb.net app which would allow me to upload the file/image
i'm using WebClient to do so.
but i'm having some difficulties passing the imgtitle parameter.
here's my code
Dim ur = New Uri("http://tnsatchat.heliohost.org/tnsatchat/upload/upload.php?title=testname")
wc.UploadFileAsync(ur, "POST", "C:\testimage.jpg")
the upload is working i.e the file is there .. but with an empty name !!
/upload/uploads/.jpg
".jpg" is supposed to be "testname.jpg" but i guess the PHP is not getting that parameter.
any hints please !
edit:
i used the QueryString property
Dim ur = New Uri("http://tnsatchat.heliohost.org/tnsatchat/upload/upload.php")
Dim col = New Collections.Specialized.NameValueCollection
col.Add("title", "testname")
wc.QueryString = col
wc.UploadFileAsync(ur, "POST", "C:\testimage.jpg")
but with no better luck !!