thongkorn โพสต์ 2019-10-30 16:03:17

[VB.NET] การทำ Line Notify และอัพโหลดรูปภาพจากตัวโปรแกรม ด้วยการใช้คำสั่ง cURL

http://www.g2gnet.com/webboard/images/vbnet/LineNotifyImage.png

cURL หรือ Client URL คือ Command Line Tools และ Library ใช้คำสั่งหรือสคริปสำหรับส่งค่าสำหรับการถ่ายโอนข้อมูลในรูปแบบ URL Syntax รวมถึงยังสนับสนุน Protocol อื่นๆ เช่น HTTP, FTP เป็นต้น ซึ่งนิยมใช้เพื่อทดสอบการ Request, Response หน้าเว็บเพจเพื่อตรวจสอบ Header และทดสอบการเรียก Webservice WSDL ของผู้ให้บริการเป็นต้น ... รายละเอียดไปหาศึกษาเพิ่มเติมด้วยล่ะกันครับ ...

ดาวน์โหลด cURL สำหรับ Windows ทั้ง 32 บิตและ 64 บิต ... เมื่อดาวน์โหลดเรียบร้อยให้แตกไฟล์ออกมา แล้วไปที่โฟลเดอร์ Bin จะมีอยู่ 3 ไฟล์ ต้องนำไฟล์ทั้ง 3 ตัวของ cURL ไปเก็บไว้ในโฟลเดอร์ Bin\debug หรือ Release ... กรณีที่ Publish ไปใช้เครื่องอื่นก็ต้องนำไปเก็บไว้ที่ EXE อยู่ด้วย
http://www.g2gnet.com/webboard/images/vbnet/LineNotifyImageCurl.png

มาดูโค้ดฉบับเต็มกันเถอะ ...' / --------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / Purpose: Line Notify and upload images with VB.NET (2010)
' / Microsoft Visual Basic .NET (2010)
' /
' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------------
Imports System.Net
Imports System.Text
Imports System.IO

Public Class frmLineNotify
    Dim streamPic As Stream   '// Use Steam instead IO.
    Dim PicturePath As String = MyPath(Application.StartupPath) & "Images\"
    '//
    Dim FullPathFileName As String = String.Empty
    '// YOUR TOKEN
    Const strToken As String = "YOUR TOKEN"

    Private Sub frmLineNotify_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      '// 20-10-2565 ... ปรับโค้ดใหม่เพื่อให้ .Net Framework ที่ต่ำกว่าเวอร์ชั่น 4.6 ทำงานได้ครับ
      System.Net.ServicePointManager.SecurityProtocol = DirectCast(3072, System.Net.SecurityProtocolType)
      '//
      txtMessage.Text = "ทดสอบการส่ง Line Notify จากคุณทองก้อน นารีแขยง"
      picData.Image = Image.FromFile(PicturePath & "NoImage.gif")
    End Sub

    Private Sub btnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
      If String.IsNullOrEmpty(txtMessage.Text.Trim) Then
            MessageBox.Show("Nothing message to send.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
      End If
      Try
            Cursor.Current = Cursors.WaitCursor
            System.Net.ServicePointManager.Expect100Continue = False
            Dim Request = DirectCast(WebRequest.Create("https://notify-api.line.me/api/notify"), HttpWebRequest)
            '// Message to Line.
            Dim LineMessage = String.Format("message={0}", txtMessage.Text & vbCrLf & "วันที่ - เวลา : " & FormatDateTime(Now(), DateFormat.GeneralDate))
            '// Sticker
            LineMessage += "&stickerPackageId=1" & "&stickerId=109"

            Dim MyData = Encoding.UTF8.GetBytes(LineMessage)
            Request.Method = "POST"
            '// Initialize
            With Request
                .ContentType = "application/x-www-form-urlencoded"
                .ContentLength = MyData.Length
                '// Change your Token and don't cut "Bearer".
                .Headers.Add("Authorization", "Bearer " & strToken)
                .AllowWriteStreamBuffering = True
                .KeepAlive = False
                .Credentials = CredentialCache.DefaultCredentials
            End With
            '//
            Using Stream = Request.GetRequestStream()
                Stream.Write(MyData, 0, MyData.Length)
            End Using
            Dim response = DirectCast(Request.GetResponse(), HttpWebResponse)
            Dim responseString = New StreamReader(response.GetResponseStream()).ReadToEnd()

            '//
            If FullPathFileName.Trim <> "" Or FullPathFileName.Length <> 0 Then Call SendPicture()

      Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
      Finally
            Cursor.Current = Cursors.Default
      End Try
    End Sub

    Sub SendPicture()
      '// Format
      '// " -X POST -H "Authorization: Bearer TOKEN" -F "message=Send Picture" -F "imageFile=@D:\Sample.jpg" https://notify-api.line.me/api/notify"
      Try
            Dim arg As String = String.Empty
            arg &= " -X POST -H "
            arg &= """Authorization: Bearer " & strToken & """"
            arg &= " -F ""message=" & "Send Picture" & """"
            arg &= " -F ""imageFile=@" & FullPathFileName.Trim & """ https://notify-api.line.me/api/notify"
            ShellandWait("curl.exe", arg)
            '//
            picData.Image = Image.FromFile(PicturePath & "NoImage.gif")
            FullPathFileName = String.Empty
      Catch ex As Exception
            MessageBox.Show("Error", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
      End Try
    End Sub

    Public Sub ShellandWait(ByVal ProcessPath As String, ByVal Arguments As String)
      Dim objProcess As System.Diagnostics.Process
      Try
            objProcess = New System.Diagnostics.Process()
            objProcess.StartInfo.Arguments = Arguments
            objProcess.StartInfo.FileName = ProcessPath
            objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            objProcess.Start()
            Application.DoEvents()
            objProcess.WaitForExit()
            Application.DoEvents()
            Console.WriteLine(objProcess.ExitCode.ToString())
            objProcess.Close()

      Catch ex As Exception
            MessageBox.Show(ex.Message)
      End Try
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
      Me.Close()
    End Sub

    Private Sub frmLineNotify_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
      Me.Dispose()
      Application.Exit()
    End Sub

    Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
      Dim dlgImage As OpenFileDialog = New OpenFileDialog()

      ' / Open File Dialog
      With dlgImage
            '.InitialDirectory = PicturePath 'PicturePath
            .Title = "Select your image file"
            .Filter = "Image (*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp"
            .FilterIndex = 1
            .RestoreDirectory = True
      End With
      '/ Select OK after Browse ...
      If dlgImage.ShowDialog() = DialogResult.OK Then
            FullPathFileName = dlgImage.FileName
            picData.Image = Image.FromFile(FullPathFileName)
      End If
    End Sub

    ' / -----------------------------------------------------------------------------
    ' / Use Steam instead IO.
    ' / -----------------------------------------------------------------------------
    Sub ShowPicture(PicName As String)
      Dim imgDB As Image
      ' Get the name of the image file.
      If PicName.ToString <> "" Then
            ' Verify that the image file meets the specified location.
            If System.IO.File.Exists(PicturePath & PicName.ToString) Then
                '/ Because when deleting the image file is locked, it can not be removed.
                '/ The file is closed after the image is loaded, so you can delete the file if you need.
                streamPic = File.OpenRead(PicturePath & PicName.ToString)
                imgDB = Image.FromStream(streamPic)
                picData.Image = imgDB
            Else
                '/ No images.
                streamPic = File.OpenRead(PicturePath & "NoImage.gif")
                imgDB = Image.FromStream(streamPic)
                picData.Image = imgDB
            End If

            ' Is null
      Else
            streamPic = File.OpenRead(PicturePath & "NoImage.gif")
            imgDB = Image.FromStream(streamPic)
            picData.Image = imgDB
      End If
      '//
      streamPic.Dispose()
    End Sub

    ' / Get my project path
    ' / AppPath = C:\My Project\bin\debug
    ' / Replace "\bin\debug" with "\"
    ' / Return : C:\My Project\
    Function MyPath(ByVal AppPath As String) As String
      '/ MessageBox.Show(AppPath);
      AppPath = AppPath.ToLower()
      '/ Return Value
      MyPath = AppPath.Replace("\bin\debug", "\").Replace("\bin\release", "\").Replace("\bin\x86\debug", "\").Replace("\bin\x86\release", "\")
      '// If not found folder then put the \ (BackSlash) at the end.
      If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
    End Function

    Private Sub btnDeleteImg_Click(sender As System.Object, e As System.EventArgs) Handles btnDeleteImg.Click
      picData.Image = Image.FromFile(PicturePath & "NoImage.gif")
      FullPathFileName = String.Empty
    End Sub
End Class


MrDen โพสต์ 2019-10-31 14:57:24

ขอบพระคุณอย่างสูงครับ อาจารย์ :$

komen โพสต์ 2019-11-19 09:17:49

ขอบพระคุณครับ อาจารย์

naiall โพสต์ 2021-1-27 19:00:25

vb6 ทำไงครับ

thongkorn โพสต์ 2021-1-28 17:24:06

ลิ้งค์ครับผม ... http://www.g2gnet.com/WebBoard/forum.php?mod=viewthread&tid=284

ooddog โพสต์ 2022-10-19 16:13:38

อาจารย์ครับทำไมรูปไม่มาใน Line ครับ มาแต่ข้อความ (โปรแกรมไม่ Error)

thongkorn โพสต์ 2022-10-20 00:35:10

ooddog ตอบกลับเมื่อ 2022-10-19 16:13
อาจารย์ครับทำไมรูปไม่มาใน Line ครับ มาแต่ข้อความ (โปรแกรมไม่ Error)

มันรันได้อยู่เหรอครับ เพราะไลน์มันเปลี่ยนโปรโตคอลใหม่ ผมเองก็ไม่ได้มาเปลี่ยนโค้ดให้ ดังนั้นสำหรับ .Net Framework ที่ต่ำกว่า 4.6 ต้องปรับโค้ดใน FormLoad ใหม่ครับ ...
System.Net.ServicePointManager.SecurityProtocol = DirectCast(3072, System.Net.SecurityProtocolType)
หน้า: [1]
ดูในรูปแบบกติ: [VB.NET] การทำ Line Notify และอัพโหลดรูปภาพจากตัวโปรแกรม ด้วยการใช้คำสั่ง cURL