vb.net convert value to string - vb.net

Not sure how to do this, here is my code
Dim oWeb As New System.Net.WebClient()
oWeb.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
Dim bytArguments As Byte() = System.Text.Encoding.ASCII.GetBytes("username=username&password=password")
Dim bytRetData As Byte() = oWeb.UploadData("https://www.website.com", "POST", bytArguments)
MsgBox(oWeb.ResponseHeaders.GetValues(2))
Im attempting to get the cookies saved then use it for another post data i want to do, but I keep getting logged off in like 1 second.
The error is
Argument 'Prompt' cannot be converted to type 'String'.
for the message box

The problem is that the GetValues returns an array of strings, which the MessageBox has trouble displaying.
When I change the line to
MsgBox(String.Join(",", oWeb.ResponseHeaders.GetValues(2)))
It works, so you need to decide how you wish to concatenate the string array that is being returned.

Related

Visual Basic .Net Public Function decodes a one line procedure before it gets executed

I encountered an issue with Function and Procedure while experimenting with some code, as below:
Module mod1
Class ExampleApp
Dim textvalue as String = "dGhpcyBpcyBhbiBleGFtcGxlIG9mIGEgdGV4dC4="
Dim string1 as String = "Convert.FromBase64String(input)"
Public Function DecodeB64(ByVal input As String) As String
Return System.Text.Encoding.UTF8.GetString(string1)
End Function
End Class
End Module
The question is, is it possible to encode the statement inside the Public Function before it gets executed?
I have seen some cases where they implemented it on PHP Scripts, where the whole script is encoded before it gets executed. I have tried my best in applying the same concept by storing "Convert.FromBase64String(input)" to a string variable but I'm encountering an issue like this:
Value of type 'String' cannot be converted to '1-Dimensional array of
Byte'
When I don't apply this concept, the text in base64 gets decoded smoothly. My main goal is that I want to obscure the statement or group of statements as much as possible. What seems to be the problem in this Error?
You have 2 mistakes below in the example code:
First, you should know that Convert.FromBase64String() returns
Byte() array, hence you can't assign it to string variable/field.
Second, Encoding.GetString() requires Byte() array as parameter,
but you're passing string to it, hence InvalidCastException occurred.
The correct usage of them should be like this:
Public Function DecodeB64(ByVal input As String) As String
' make sure the input string is Base64 formatted
Dim bytearray As Byte() = Convert.FromBase64String(input)
' decoding from byte array
Return System.Text.Encoding.UTF8.GetString(bytearray)
End Function
' usage
Dim textvalue as String = "dGhpcyBpcyBhbiBleGFtcGxlIG9mIGEgdGV4dC4="
Dim result As String = DecodeB64(textvalue)
Working example: .NET Fiddle demo

Read Data From The Byte Array Returned From Web Service

I have a web service,which return data in byte array.Now i want to read that data in my console project.How can i do that,i already add the desire references to access that web service.I am using vb.net VS2012.Thanks.My web service method is as follow.
Public Function GetFile() As Byte()
Dim response As Byte()
Dim filePath As String = "D:\file.txt"
response = File.ReadAllBytes(filePath)
Return response
End Function
Something like,
Dim result As String
Using (Dim data As New MemoryStream(response))
Using (Dim reader As New StreamReader(data))
result = reader.ReadToEnd()
End Using
End Using
if you knew the encoding, lets say it was UTF-8 you could do,
Dim result = System.Text.UTF8Encoding.GetString(response)
Following on from your comments, I think you are asserting this.
Dim response As Byte() 'Is the bytes of a Base64 encoded string.
So, we know all the bytes will be valid ASCII (because its Base64,) so the string encoding is interchangable.
Dim base64Encoded As String = System.Text.UTF8Encoding.GetString(response)
Now, base64Encoded is the string Base64 representation of some binary.
Dim decodedBinary As Byte() = Convert.FromBase64String(base64Encoded)
So, we've changed the encoded base64 into the binary it represents. Now, because I can see that in your example, you are reading a file called "D:/file.txt" I'm going to make the assumption that the contents of the file is a character encoded string, but I don't know the encoding of the string. The StreamReader class has some logic in the constructor that can make an educated guess at character encoding.
Dim result As String
Using (Dim data As New MemoryStream(decodedBinary))
Using (Dim reader As New StreamReader(data))
result = reader.ReadToEnd()
End Using
End Using
Hopefully, now result contains the context of the text file.

How to assign a value to an array from a combobox

The code I have is:
Dim Dbase() As String = Nothing
Dbase(0) = Db_ComboBox.Text
I have declared Dbase as array and assigned Nothing, Db_ComboBox is a combobox.
For that assignment statement, I'm getting the following error: "Reference 'Dbase' has a value of 'Nothing'"
What is the reason for this error, and how can I take the value from the combobox and save it in the array?
You need to change this:
Dim Dbase() As String = Nothing
to this (declare an array of 1 element):
Dim Dbase(0) As String
And then this line will work:
Dbase(0) = Db_ComboBox.Text
If you need to change your array size you can use Redim or Redim preserve, as required.
If you anticipate contents of Dbase to change often, I am all with #Joel's suggestion about switching to List(Of String) instead of handling array sizes manually.
Let's look at your code:
Dim Dbase() As String = Nothing
Dbase(0) = Db_ComboBox.Text
Especially the first line. That first line creates a variable that can refer to an array, but the = Nothing portion explicitly tells it, "Do not create a real array here yet". You have, effectively, a pointer that doesn't point to anything.
I get here that what you really need is a List collection that you can append to over time:
Dim Dbase As New List(Of String)()
Dbase.Add(Db_ComboBox.Text)
Dbase() IS NOTHING. Look at this example:
cargoWeights = New Double(10) {}
atmospherePressures = New Short(2, 2, 4, 10) {}
inquiriesByYearMonthDay = New Byte(20)()() {}
That's how you declare arrays.
More examples: http://msdn.microsoft.com/en-us/library/vstudio/wak0wfyt.aspx

splitting a string in VB

I have a ComboBox which I assign to a variable:
Dim var as String = ComboBox1.SelectedValue
Dim name As String = var.Split(",")
This gives me the error
Value of type '1-dimensional array of string' cannot be converted to String
Any ideas as to where I'm going wrong?
Split returns an array of strings. Your variable needs to be changed to an array, not just a single string.
My VB is a bit rusty, but I think you have to make name an array:
Dim name() As String = var.Split(",")
name needs to be declared as an array.
dim name() as string = var.split(",")
The split() method will break up the string based on the given character and put each newly created string into an array and return it.
This is what your error message is telling you:
Value of type '1-dimensional array of string' cannot be converted to String
The method returns an array of string, but your trying to put it into just a string!
EDIT: In response to your answer...
So far you've managed to split the string yourself with the split method. To output this to your message box, you need to concatenate the two elements in the proper order:
msgbox(name(1) & " " & name(0))
Notice I indexed the array twice! Element 1 is the first name, element 0 is the last name. Remember you got this name in lname,fname format. Passing the array itself doesn't make sense! Remember, a datatype is not equal to an array of that type, they are two different things. Therefore a string is not compatible with a string array. However, each individual element of the array is a string, and so each of those are compatible with the string type (because they're the same thing)!
Dim var As String = ComboBox1.SelectedValue
Dim temp() As String = Split(var, ",", -1, CompareMethod.Binary)
Dim name As String = temp(0)
Or maybe "name" isn't an array and the goal is to populate "name" with everything up until the first comma, in which case the fix would be:
Dim name as String = var.Split(",")(0)
Note: assumes that var is not Nothing.

Deserialize a Digital Persona template in VB.net

Reading binary data out of the database, and I need to convert it back into a Digital Persona fingerprint template. I'm not familiar with serialization and deserialization, so I could use a bit of help. Here's what I tried:
Dim rsBioData As SqlDataReader = SQL.ExecuteReader
Dim byteTemplate As Byte
Dim memStreamTemplate As MemoryStream
If rsBioData.HasRows Then
While rsBioData.Read
byteTemplate = rsBioData("BiometricData")
memStreamTemplate = New MemoryStream(byteTemplate)
Me.Template = DirectCast(template.DeSerialize(memStreamTemplate), DPFP.Template)
End While
End If
rsBioData.Close()
I receive an error that template.DeSerialize(memStreamTemplate) does not create a value.
For kicks, here's how I serialized the object to place it into the database. I assume this part is working, since the binary data shows up in SQL server--just can't read it back out to see.
Dim str As New MemoryStream
Enroller.Template.Serialize(str)
Dim serializedTemplate As Byte() = str.ToArray()
SQL.Parameters.AddWithValue("biometricData", serializedTemplate)
Thanks
Here's how I was finally able to do it. I was SO close the first time around.
byteTemplate = rsBioData("BiometricData")
memStreamTemplate = New MemoryStream(byteTemplate)
Me.Template.DeSerialize(memStreamTemplate)