BioloMICS logo
×
BioloMICS menu

Call to Mycobank

 
A call to Mycobank by species name is given below.
 
This code will prompt the user for a species name and run a first query using that name.
 
If multiple records are received from Mycobank, the code will ask the user to choose which species to display.
 
The corresponding URL is finally copied into the clipboard. This example is complete.
 
Imports System
Imports System.Collections
Imports System.Text     '     for StringBuilder
Imports Biolomics.BioCallback
Imports Biolomics.SharedClasses
 
Imports System.windows.forms     '     for class Clipboard
 
Public Class MyNewClass
 
     ' This function is called when running this script
     Shared Sub Main()
 
          Dim c As New MyNewClass
 
          '     Create an instance of class MyNewClass and call its functions
          'Run.RunTest()
 
          c.MycobankQuery()
     End Sub
 
     Public Sub MycobankQuery()
          Dim SpeciesName As String = InputBox("Species name to search for?")
          If String.IsNullOrEmpty(SpeciesName) Then
               Return
          End If
 
          Dim TextLine As String = "http://www.mycobank.org/BioloMICSServer.aspx?Link=T&NameIs=" & SpeciesName
 
          '     build the HTTP request
          Dim Req As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create(TextLine), System.Net.HttpWebRequest)
          Req.Headers.Clear()
          Req.Headers.Add("GET", " HTTP/1.0")
          Req.Timeout = 5000
          Req.ContentType = "application/x-www-form-urlencoded"
 
          Dim temp As String = ""
 
          Dim loWebResponse As System.Net.WebResponse = Nothing
          Dim loResponseStream As System.IO.StreamReader = Nothing
          Try
               '     run the request
               loWebResponse = Req.GetResponse()
               Dim enc As System.Text.Encoding = System.Text.Encoding.GetEncoding(1252) ' // Windows default Code Page
               loResponseStream = New System.IO.StreamReader(loWebResponse.GetResponseStream(), enc)
               temp = loResponseStream.ReadToEnd()
               loWebResponse.Close()
               loResponseStream.Close()
          Catch ex As Exception
               If loWebResponse IsNot Nothing Then
                    loWebResponse.Close()
               End If
               If loResponseStream IsNot Nothing Then
                    loResponseStream.Close()
               End If
               Console.Write("Erreur de requĂȘte: " & ex.Message)
               Return
          End Try
 
          '     read the request result
          Dim ListOfResults As Generic.List(Of String) = vSplit(temp, "|"c)
          Dim Lines As Generic.List(Of String) = vSplit(temp, "@@")
          If ListOfResults.Count = 0 OrElse Lines.Count < 3 Then
               Return
          End If
          Dim MycobankName As String = Lines(0)
          Dim MycobankID As String = Lines(2)
 
          '     if multiple results, prompt user
          If ListOfResults.Count >= 2 Then
               Dim SelectedName As String = ""
               Dim i As Integer = 1
               For Each TempText As String In ListOfResults
                    SelectedName &= i.ToString & ". " & TempText.Replace("@@", " ") & vbCrLf
                    i += 1
               Next
               SelectedName = InputBox("Select the number of the species to be used: " & vbCrLf & vbCrLf & SelectedName)
               If String.IsNullOrEmpty(SelectedName) Then
                    Return
               End If
 
               Dim FinalSelection As String = ListOfResults(CInt(SelectedName) - 1)
               Lines = vSplit(FinalSelection, "@@")
               If Lines.Count < 3 Then
                    Return
               End If
               MycobankName = Lines(0)
               MycobankID = Lines(2)
          End If
 
          '     copy the Mycobank URL into the clipboard
          Clipboard.Clear()
          Clipboard.SetText("http://www.mycobank.org/MycoTaxo.aspx?Link=T&Rec=" & MycobankID)
          Console.Write("The Mycobank URL has been copied in the Clipboard")
     End Sub
 
End Class