michelepizzi

Implementare la creazione di file pastebin

Jul 19th, 2013 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.80 KB | None | 0 0
  1. 'Implementare la creazione di file pastebin
  2. 'Per i parametri da usare visualizzare il pastebin "Pastebin API parameter" http://pastebin.com/QMfR6gpJ
  3. 'For API Key: http://pastebin.com/api
  4.  
  5.     'Funzione per creare il file Pastebin
  6.     'Function to make a Pastebin file
  7.     Public Function NewPaste(ByVal Content As String)
  8.         Dim api_dev_key As String = "" '<-- La tua API key \ Your API Key
  9.         Dim api_user_name As String = "" '<-- Il tuo Username \ Your Username
  10.         Dim api_user_password As String = "" '<-- La tua Password \ Your Password
  11.         Dim api_paste_code As String = URLEncode(Content)
  12.         Dim api_paste_private As String = "2" '<-- Public=0, Unlisted=1, Private=2
  13.         Dim api_paste_name As String = URLEncode("") '<-- Il nome per il file di testo da creare \ Pastebin File Name
  14.         Dim api_paste_expire_date As String = "N" '<-- Durata del file (N = Mai) \ Expire Date (N = Never)
  15.         Dim api_paste_format As String = "text" '<-- Formato (text = None) \ Format (text = None
  16.         Dim api_user_key As String = HttpPost("http://pastebin.com/api/api_login.php", "api_dev_key=" & api_dev_key & "&api_user_name=" & api_user_name & "&api_user_password=" & api_user_password)
  17.         Dim Response As String = HttpPost("http://pastebin.com/api/api_post.php", "api_option=paste&api_user_key=" & api_user_key & "&api_paste_private=" & api_paste_private & "&api_paste_name=" & api_paste_name & "&api_paste_expire_date=" & api_paste_expire_date & "&api_paste_format=" & api_paste_format & "&api_dev_key=" & api_dev_key & "&api_paste_code=" & api_paste_code)
  18.  
  19.     'Se il server crea il pastebin produce il link in formato Raw altrimenti dà Error
  20.     'If the server build the pastebin, the function produces the link in Raw format, otherwise it gives Error
  21.         If Response.Contains("Bad API request") = False Then
  22.             Return Raw(Response)
  23.         Else
  24.             Return "Error"
  25.         End If
  26.     End Function
  27.  
  28.     'La funzione UrlEncode serve per non inserire errori nella stringa da dare all'URL
  29.     'The function UrlEncode need to delete errors in the string to give the URL
  30.     Private Function URLEncode(ByVal EncodeStr As String) As String
  31.         Dim i As Integer
  32.         Dim erg As String
  33.         erg = EncodeStr
  34.         erg = Replace(erg, "%", Chr(1))
  35.         erg = Replace(erg, "+", Chr(2))
  36.         For i = 0 To 255
  37.             Select Case i
  38.                 Case 37, 43, 48 To 57, 65 To 90, 97 To 122
  39.                 Case 1
  40.                     erg = Replace(erg, Chr(i), "%25")
  41.                 Case 2
  42.                     erg = Replace(erg, Chr(i), "%2B")
  43.                 Case 32
  44.                     erg = Replace(erg, Chr(i), "+")
  45.                 Case 3 To 15
  46.                     erg = Replace(erg, Chr(i), "%0" & Hex(i))
  47.                 Case Else
  48.                     erg = Replace(erg, Chr(i), "%" & Hex(i))
  49.             End Select
  50.         Next
  51.         Return erg
  52.     End Function
  53.  
  54.  
  55.     'La funzione Raw restituisce l'URL del pastebin in formato Raw
  56.     'The function Raw returns the URL of the pastebin in Raw format
  57.     Public Function Raw(ByVal URL As String)
  58.         Dim ID As String = URL.Substring(URL.LastIndexOf("/") + 1)
  59.         ID = "http://pastebin.com/raw.php?i=" & ID
  60.         Return ID
  61.     End Function
  62.  
  63.  
  64.     'La funzione HttpPost Serve per mandare le chiamate al server
  65.     'The HttpPost function is used to send calls to the server
  66.     Private Function HttpPost(ByVal URL As String, ByVal Data As String)
  67.         Dim request As WebRequest = WebRequest.Create(URL)
  68.         request.Method = "POST"
  69.         Dim byteArray As Byte() = Encoding.UTF8.GetBytes(Data)
  70.         request.ContentType = "application/x-www-form-urlencoded"
  71.         request.ContentLength = byteArray.Length
  72.         Dim dataStream As Stream = request.GetRequestStream()
  73.         dataStream.Write(byteArray, 0, byteArray.Length)
  74.         dataStream.Close()
  75.         Dim response As WebResponse = request.GetResponse()
  76.         'Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
  77.         dataStream = response.GetResponseStream()
  78.         Dim reader As New StreamReader(dataStream)
  79.         Dim responseFromServer As String = reader.ReadToEnd()
  80.         reader.Close()
  81.         dataStream.Close()
  82.         response.Close()
  83.         Return responseFromServer
  84.     End Function
  85.  
  86.  
  87.  
  88.     'In seguito puoi implementare le funzioni per poter, per esempio, generare il link in una Textbox premendo su un pulsante
  89.     'Later can implement the functions to be able to, for example, generate the link in a Textbox by pressing on a button
  90.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  91.         Dim pastebin_string As String = "Text File Content by Pizzul" + vbNewLine + vbNewLine + "Thanks for this"
  92.         Textbox1.Text = NewPaste(pastebin_string)
  93.     End Sub
Add Comment
Please, Sign In to add comment