Answered by:
Java function to vb.net

Question
-
User82651084 posted
I have been given a project where I must convert a java class into vb.net for itunes U, unfortunately I know c# and vb but not java. Here is what i have found online to so far:
/** * Send a request for an action to iTunes U with an authorization token. * * @param url URL defining how to communicate with iTunes U and * identifying which iTunes U action to invoke and which * iTunes U page or item to apply the action to. Such URLs have * a format like <CODE>[PREFIX]/[ACTION]/[DESTINATION]</CODE>, * where <CODE>[PREFIX]</CODE> is a value like * "https://deimos.apple.com/WebObjects/Core.woa" which defines * how to communicate with iTunes U, <CODE>[ACTION]</CODE> is * a value like "Browse" which identifies which iTunes U action * to invoke, and <CODE>[DESTINATION]</CODE> is a value like * "example.edu" which identifies which iTunes U page or item to * apply the action to. The destination string "example.edu" * refers to the root page of the iTunes U site identified by * the domain "example.edu". Destination strings for other items * within that site contain the site domain followed by * numbers separated by periods. For example: * "example.edu.123.456.0789". You can find these strings in the * items' URLs, which you can obtain from iTunes. See the * iTunes U documentation for details. * @param token Authorization token generated by getAuthorizationToken(). * * @return The iTunes U response, which may be HTML or * text depending on the type of action invoked. */ public String invokeAction(String url, String token) { // Send a request to iTunes U and record the response. StringBuffer response = null; try { // Verify that the communication will be over SSL. if (!url.startsWith("https")) { throw new MalformedURLException( "ITunesU.invokeAction(): URL \"" + url + "\" does not use HTTPS."); } // Create a connection to the requested iTunes U URL. HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection(); connection.setUseCaches(false); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // Send the authorization token to iTunes U. connection.connect(); OutputStream output = connection.getOutputStream(); output.write(token.getBytes("UTF-8")); output.flush(); output.close(); // Read iTunes U's response. response = new StringBuffer(); InputStream input = connection.getInputStream(); Reader reader = new InputStreamReader(input, "UTF-8"); reader = new BufferedReader(reader); char[] buffer = new char[16 * 1024]; for (int n = 0; n >= 0;) { n = reader.read(buffer, 0, buffer.length); if (n > 0) response.append(buffer, 0, n); } // Clean up. input.close(); connection.disconnect(); } catch (UnsupportedEncodingException e) { // ITunes U requires UTF-8 and ASCII encoding support. throw new java.lang.AssertionError( "ITunesU.invokeAction(): UTF-8 encoding not supported!"); } catch (IOException e) { // Report communication problems. throw new java.lang.AssertionError( "ITunesU.invokeAction(): I/O Exception " + e); } // Return the response received from iTunes U. return response.toString(); }
I have converted about half of this into a vb class but I can't find anything in .net that will work for HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection(); and
throw new java.lang.AssertionError. Any help someone can give in converting this is greatly appreciated.
Tuesday, February 16, 2010 7:27 PM
Answers
-
User-1659704165 posted
Hi,
have tried using
HttpWebRequest,
http://msdn.microsoft.com/en-us/library/system.net.webrequest%28VS.71%29.aspx- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, February 16, 2010 11:27 PM -
User-952121411 posted
I have converted about half of this into a vb class but I can't find anything in .net that will work for HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection(); andthrow new java.lang.AssertionError. Any help someone can give in converting this is greatly appreciated.
The 'throw new...' code is just the developer throwing a custom exception message of type 'AssertionError'. You need to do something similar with the equivalent or similar type exception in .NET. Take a look to the following link that discusses the equivalent:
.Net equivalent to Java’s AssertionError:
http://stackoverflow.com/questions/1254044/net-equivalent-to-javas-assertionerror
As briefly eluded to in the previous post, take a look to the HttpRequest class in .NET. Here are some better links (especially the 1st link) that will help get you started and mapping those settings from the JAVA code into the .NET Equivalent:
How to: Send Data Using the WebRequest Class:<!---->http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
HttpWebRequest Class:<!---->Hope this helps!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 17, 2010 10:06 AM
All replies
-
User-1659704165 posted
Hi,
have tried using
HttpWebRequest,
http://msdn.microsoft.com/en-us/library/system.net.webrequest%28VS.71%29.aspx- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, February 16, 2010 11:27 PM -
User-952121411 posted
I have converted about half of this into a vb class but I can't find anything in .net that will work for HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection(); andthrow new java.lang.AssertionError. Any help someone can give in converting this is greatly appreciated.
The 'throw new...' code is just the developer throwing a custom exception message of type 'AssertionError'. You need to do something similar with the equivalent or similar type exception in .NET. Take a look to the following link that discusses the equivalent:
.Net equivalent to Java’s AssertionError:
http://stackoverflow.com/questions/1254044/net-equivalent-to-javas-assertionerror
As briefly eluded to in the previous post, take a look to the HttpRequest class in .NET. Here are some better links (especially the 1st link) that will help get you started and mapping those settings from the JAVA code into the .NET Equivalent:
How to: Send Data Using the WebRequest Class:<!---->http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
HttpWebRequest Class:<!---->Hope this helps!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 17, 2010 10:06 AM -
User82651084 posted
I figured it out. I was making it harder than it actually was. Here is the code I used:
Public Shared Function ItunesU_login(ByVal nameString As String, ByVal emailString As String, ByVal usernameString As String, ByVal useridString As String) As String
' Credential string
Dim credentialString As String = "Administrator@urn:mace:itunesu.com:sites:uic.edu"
credentialString = "credentials=" & credentialString
If nameString.Length > 0 Then
nameString = "'" + nameString + "'"
End If
If emailString.Length > 0 Then
emailString = "<" + emailString + ">"
End If
If usernameString.Length > 0 Then
usernameString = "(" + usernameString + ")"
End If
If useridString.Length > 0 Then
useridString = "[" + useridString + "]"
End If
Dim identityString As String = nameString + emailString + usernameString + useridString
identityString = "identity=" + identityString
' Time string
Dim epoch As New DateTime(1970, 1, 1, 0, 0, 0, _
DateTimeKind.Utc)
Dim now As DateTime = DateTime.UtcNow
Dim interval As TimeSpan = now - epoch
Dim seconds As Integer = CInt(interval.TotalSeconds)
Dim timeString As String = "time=" & seconds
' Token string (so far)
Dim tokenString As String = ((credentialString & " & ") + identityString & " & ") + timeString
tokenString = tokenString.Replace(" ", "+")
tokenString = tokenString.Replace("'", "%23")
tokenString = tokenString.Replace("(", "%28")
tokenString = tokenString.Replace(")", "%29")
tokenString = tokenString.Replace(":", "%3A")
tokenString = tokenString.Replace("<", "%3C")
tokenString = tokenString.Replace(">", "%3E")
tokenString = tokenString.Replace("@", "%40")
tokenString = tokenString.Replace("[", "%5B")
tokenString = tokenString.Replace("]", "%5D")
'Compute signature string
Dim sharedSecret As String = "SHAREDSECRETWITH32CHARACTERSSINIT"
Dim encodedKey As Byte() = System.Text.Encoding.UTF8.GetBytes(sharedSecret)
Dim hmac As System.Security.Cryptography.HMACSHA256 = New System.Security.Cryptography.HMACSHA256(encodedKey)
Dim digest As Byte() = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(tokenString))
Dim digestString As String = ToHexString(digest)
digestString = digestString.ToLower(System.Globalization.CultureInfo.InvariantCulture)
Dim signatureString = "signature=" + digestString
' Token string (final)
tokenString = (tokenString & " & ") + signatureString
Dim iTunesURLString As String = ("https://deimos.apple.com/WebObjects/Core.woa/Browse/uic.edu" & "?") + tokenString
Return iTunesURLString
End Function
Public Shared Function ToHexString(ByVal bytes As Byte()) As String
Dim hexString As String = ""
For i As Integer = 0 To bytes.Length - 1
Dim num As Integer = bytes(i)
hexString += [String].Format("{0:X2}", num)
Next
Return hexString
End Function
' Credential stringDim credentialString As String = "Administrator@urn:mace:itunesu.com:sites:uic.edu"credentialString = "credentials=" & credentialStringIf nameString.Length > 0 ThennameString = "'" + nameString + "'"End IfIf emailString.Length > 0 ThenemailString = "<" + emailString + ">"End IfIf usernameString.Length > 0 ThenusernameString = "(" + usernameString + ")"End IfIf useridString.Length > 0 ThenuseridString = "[" + useridString + "]"End IfDim identityString As String = nameString + emailString + usernameString + useridStringidentityString = "identity=" + identityString' Time stringDim epoch As New DateTime(1970, 1, 1, 0, 0, 0, _DateTimeKind.Utc)Dim now As DateTime = DateTime.UtcNowDim interval As TimeSpan = now - epochDim seconds As Integer = CInt(interval.TotalSeconds)Dim timeString As String = "time=" & seconds' Token string (so far)Dim tokenString As String = ((credentialString & " & ") + identityString & " & ") + timeStringtokenString = tokenString.Replace(" ", "+")tokenString = tokenString.Replace("'", "%23")tokenString = tokenString.Replace("(", "%28")tokenString = tokenString.Replace(")", "%29")tokenString = tokenString.Replace(":", "%3A")tokenString = tokenString.Replace("<", "%3C")tokenString = tokenString.Replace(">", "%3E")tokenString = tokenString.Replace("@", "%40")tokenString = tokenString.Replace("[", "%5B")tokenString = tokenString.Replace("]", "%5D")'Compute signature stringDim sharedSecret As String = "SHAREDSECRETWITH32CHARACTERSSINIT"Dim encodedKey As Byte() = System.Text.Encoding.UTF8.GetBytes(sharedSecret)Dim hmac As System.Security.Cryptography.HMACSHA256 = New System.Security.Cryptography.HMACSHA256(encodedKey)Dim digest As Byte() = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(tokenString))Dim digestString As String = ToHexString(digest)digestString = digestString.ToLower(System.Globalization.CultureInfo.InvariantCulture)Dim signatureString = "signature=" + digestString' Token string (final)tokenString = (tokenString & " & ") + signatureStringDim iTunesURLString As String = ("https://deimos.apple.com/WebObjects/Core.woa/Browse/uic.edu" & "?") + tokenStringReturn iTunesURLStringEnd FunctionPublic Shared Function ToHexString(ByVal bytes As Byte()) As StringDim hexString As String = ""For i As Integer = 0 To bytes.Length - 1Dim num As Integer = bytes(i)hexString += [String].Format("{0:X2}", num)NextReturn hexStringEnd FunctionPublic Shared Function ItunesU_login(ByVal nameString As String, ByVal emailString As String, ByVal usernameString As String, ByVal useridString As String) As String' Credential stringDim credentialString As String = "Administrator@urn:mace:itunesu.com:sites:uic.edu"credentialString = "credentials=" & credentialStringIf nameString.Length > 0 ThennameString = "'" + nameString + "'"End IfIf emailString.Length > 0 ThenemailString = "<" + emailString + ">"End IfIf usernameString.Length > 0 ThenusernameString = "(" + usernameString + ")"End IfIf useridString.Length > 0 ThenuseridString = "[" + useridString + "]"End IfDim identityString As String = nameString + emailString + usernameString + useridStringidentityString = "identity=" + identityString' Time stringDim epoch As New DateTime(1970, 1, 1, 0, 0, 0, _DateTimeKind.Utc)Dim now As DateTime = DateTime.UtcNowDim interval As TimeSpan = now - epochDim seconds As Integer = CInt(interval.TotalSeconds)Dim timeString As String = "time=" & seconds' Token string (so far)Dim tokenString As String = ((credentialString & " & ") + identityString & " & ") + timeStringtokenString = tokenString.Replace(" ", "+")tokenString = tokenString.Replace("'", "%23")tokenString = tokenString.Replace("(", "%28")tokenString = tokenString.Replace(")", "%29")tokenString = tokenString.Replace(":", "%3A")tokenString = tokenString.Replace("<", "%3C")tokenString = tokenString.Replace(">", "%3E")tokenString = tokenString.Replace("@", "%40")tokenString = tokenString.Replace("[", "%5B")tokenString = tokenString.Replace("]", "%5D")'Compute signature stringDim sharedSecret As String = "SHAREDSECRETWITH32CHARACTERSSINIT"Dim encodedKey As Byte() = System.Text.Encoding.UTF8.GetBytes(sharedSecret)Dim hmac As System.Security.Cryptography.HMACSHA256 = New System.Security.Cryptography.HMACSHA256(encodedKey)Dim digest As Byte() = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(tokenString))Dim digestString As String = ToHexString(digest)digestString = digestString.ToLower(System.Globalization.CultureInfo.InvariantCulture)Dim signatureString = "signature=" + digestString' Token string (final)tokenString = (tokenString & " & ") + signatureStringDim iTunesURLString As String = ("https://deimos.apple.com/WebObjects/Core.woa/Browse/uic.edu" & "?") + tokenStringReturn iTunesURLStringEnd FunctionPublic Shared Function ToHexString(ByVal bytes As Byte()) As StringDim hexString As String = ""For i As Integer = 0 To bytes.Length - 1Dim num As Integer = bytes(i)hexString += [String].Format("{0:X2}", num)NextReturn hexStringEnd Function
Wednesday, April 14, 2010 3:13 PM