ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 8886|ตอบกลับ: 6

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

[คัดลอกลิงก์]

308

กระทู้

498

โพสต์

5973

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
5973



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 อยู่ด้วย


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

  16. Public Class frmLineNotify
  17.     Dim streamPic As Stream     '// Use Steam instead IO.
  18.     Dim PicturePath As String = MyPath(Application.StartupPath) & "Images"
  19.     '//
  20.     Dim FullPathFileName As String = String.Empty
  21.     '// YOUR TOKEN
  22.     Const strToken As String = "YOUR TOKEN"

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

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

  43.             Dim MyData = Encoding.UTF8.GetBytes(LineMessage)
  44.             Request.Method = "POST"
  45.             '// Initialize
  46.             With Request
  47.                 .ContentType = "application/x-www-form-urlencoded"
  48.                 .ContentLength = MyData.Length
  49.                 '// Change your Token and don't cut "Bearer".
  50.                 .Headers.Add("Authorization", "Bearer " & strToken)
  51.                 .AllowWriteStreamBuffering = True
  52.                 .KeepAlive = False
  53.                 .Credentials = CredentialCache.DefaultCredentials
  54.             End With
  55.             '//
  56.             Using Stream = Request.GetRequestStream()
  57.                 Stream.Write(MyData, 0, MyData.Length)
  58.             End Using
  59.             Dim response = DirectCast(Request.GetResponse(), HttpWebResponse)
  60.             Dim responseString = New StreamReader(response.GetResponseStream()).ReadToEnd()

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

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

  69.     Sub SendPicture()
  70.         '// Format
  71.         '// " -X POST -H "Authorization: Bearer TOKEN" -F "message=Send Picture" -F "imageFile=@D:\Sample.jpg" https://notify-api.line.me/api/notify"
  72.         Try
  73.             Dim arg As String = String.Empty
  74.             arg &= " -X POST -H "
  75.             arg &= """Authorization: Bearer " & strToken & """"
  76.             arg &= " -F ""message=" & "Send Picture" & """"
  77.             arg &= " -F ""imageFile=@" & FullPathFileName.Trim & """ https://notify-api.line.me/api/notify"
  78.             ShellandWait("curl.exe", arg)
  79.             '//
  80.             picData.Image = Image.FromFile(PicturePath & "NoImage.gif")
  81.             FullPathFileName = String.Empty
  82.         Catch ex As Exception
  83.             MessageBox.Show("Error", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
  84.         End Try
  85.     End Sub

  86.     Public Sub ShellandWait(ByVal ProcessPath As String, ByVal Arguments As String)
  87.         Dim objProcess As System.Diagnostics.Process
  88.         Try
  89.             objProcess = New System.Diagnostics.Process()
  90.             objProcess.StartInfo.Arguments = Arguments
  91.             objProcess.StartInfo.FileName = ProcessPath
  92.             objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
  93.             objProcess.Start()
  94.             Application.DoEvents()
  95.             objProcess.WaitForExit()
  96.             Application.DoEvents()
  97.             Console.WriteLine(objProcess.ExitCode.ToString())
  98.             objProcess.Close()

  99.         Catch ex As Exception
  100.             MessageBox.Show(ex.Message)
  101.         End Try
  102.     End Sub

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

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

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

  112.         ' / Open File Dialog
  113.         With dlgImage
  114.             '.InitialDirectory = PicturePath 'PicturePath
  115.             .Title = "Select your image file"
  116.             .Filter = "Image (*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp"
  117.             .FilterIndex = 1
  118.             .RestoreDirectory = True
  119.         End With
  120.         '/ Select OK after Browse ...
  121.         If dlgImage.ShowDialog() = DialogResult.OK Then
  122.             FullPathFileName = dlgImage.FileName
  123.             picData.Image = Image.FromFile(FullPathFileName)
  124.         End If
  125.     End Sub

  126.     ' / -----------------------------------------------------------------------------
  127.     ' / Use Steam instead IO.
  128.     ' / -----------------------------------------------------------------------------
  129.     Sub ShowPicture(PicName As String)
  130.         Dim imgDB As Image
  131.         ' Get the name of the image file.
  132.         If PicName.ToString <> "" Then
  133.             ' Verify that the image file meets the specified location.
  134.             If System.IO.File.Exists(PicturePath & PicName.ToString) Then
  135.                 '/ Because when deleting the image file is locked, it can not be removed.
  136.                 '/ The file is closed after the image is loaded, so you can delete the file if you need.
  137.                 streamPic = File.OpenRead(PicturePath & PicName.ToString)
  138.                 imgDB = Image.FromStream(streamPic)
  139.                 picData.Image = imgDB
  140.             Else
  141.                 '/ No images.
  142.                 streamPic = File.OpenRead(PicturePath & "NoImage.gif")
  143.                 imgDB = Image.FromStream(streamPic)
  144.                 picData.Image = imgDB
  145.             End If

  146.             ' Is null
  147.         Else
  148.             streamPic = File.OpenRead(PicturePath & "NoImage.gif")
  149.             imgDB = Image.FromStream(streamPic)
  150.             picData.Image = imgDB
  151.         End If
  152.         '//
  153.         streamPic.Dispose()
  154.     End Sub

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

  167.     Private Sub btnDeleteImg_Click(sender As System.Object, e As System.EventArgs) Handles btnDeleteImg.Click
  168.         picData.Image = Image.FromFile(PicturePath & "NoImage.gif")
  169.         FullPathFileName = String.Empty
  170.     End Sub
  171. End Class
คัดลอกไปที่คลิปบอร์ด


ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

51

โพสต์

233

เครดิต

Full Member

Rank: 3Rank: 3

เครดิต
233
โพสต์ 2019-10-31 14:57:24 | ดูโพสต์ทั้งหมด

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

4

กระทู้

13

โพสต์

102

เครดิต

Member

Rank: 2

เครดิต
102
โพสต์ 2019-11-19 09:17:49 | ดูโพสต์ทั้งหมด

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

1

กระทู้

4

โพสต์

77

เครดิต

Member

Rank: 2

เครดิต
77
โพสต์ 2021-1-27 19:00:25 | ดูโพสต์ทั้งหมด

vb6 ทำไงครับ

308

กระทู้

498

โพสต์

5973

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
5973
 เจ้าของ| โพสต์ 2021-1-28 17:24:06 | ดูโพสต์ทั้งหมด

ลิ้งค์ครับผม ... http://www.g2gnet.com/WebBoard/f ... wthread&tid=284
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

1

กระทู้

7

โพสต์

93

เครดิต

Member

Rank: 2

เครดิต
93
โพสต์ 2022-10-19 16:13:38 | ดูโพสต์ทั้งหมด

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

308

กระทู้

498

โพสต์

5973

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
5973
 เจ้าของ| โพสต์ 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)
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-3-29 13:53 , Processed in 0.225689 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้