1 #Region "Microsoft.VisualBasic::5b37ca2ecbcbc5969c0b2eb7bdb64de2, Microsoft.VisualBasic.Core\Net\HTTP\Multipart.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class MultipartForm
35     
36     '         Function: POST
37     
38     '         Sub: (+2 Overloads) Add, Dump
39     
40     
41     ' /********************************************************************************/
42
43 #End Region
44
45 Imports System.IO
46 Imports System.Net
47 Imports System.Runtime.CompilerServices
48 Imports System.Text
49 Imports Microsoft.VisualBasic.Language
50 Imports Microsoft.VisualBasic.Linq
51 Imports Microsoft.VisualBasic.Net.Protocols
52
53 Namespace Net.Http
54
55     ' https://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data
56
57     Public Class MultipartForm
58
59         ReadOnly buffer As New MemoryStream
60         ''' <summary>
61         ''' 需要使用<see cref="Encoding.ASCII"/>来进行编码
62         ''' </summary>
63         ReadOnly boundary$ = "---------------------------" & DateTime.Now.Ticks.ToString("x")
64
65         ''' <summary>
66         ''' Add form data.(添加键值对数据)
67         ''' </summary>
68         ''' <param name="name$"></param>
69         ''' <param name="value$"></param>
70         ''' <remarks>
71         ''' 字符串键值对是使用<see cref="Encoding.UTF8"/>编码的
72         ''' </remarks>
73         Public Sub Add(name$, value$)
74             With Me.buffer
75                 Call .WriteLine(, Encoding.ASCII)
76                 Call .WriteLine("--" & boundary, Encoding.ASCII)
77                 Call .WriteLine($"Content-Disposition: form-data; name=""{name}""")
78                 Call .WriteLine()
79                 Call .Write(value)
80             End With
81         End Sub
82
83         ''' <summary>
84         ''' 添加文件或者数据包等
85         ''' </summary>
86         ''' <param name="name$"></param>
87         ''' <param name="buffer"></param>
88         ''' <remarks>
89         ''' 
90         ''' </remarks>
91         Public Sub Add(name$,
92                        buffer As IEnumerable(Of Byte),
93                        Optional fileName$ = "UnknownStream",
94                        Optional contentType$ = ContentTypes.Unknown)
95
96             With Me.buffer
97                 Call .WriteLine(, Encoding.ASCII)
98                 Call .WriteLine("--" & boundary, Encoding.ASCII)
99                 Call .WriteLine($"Content-Disposition: form-data; name=""{name}""; filename=""{fileName}""")
100                 Call .WriteLine($"Content-Type: {contentType}")
101                 Call .WriteLine()
102             End With
103
104             With buffer.ToArray
105                 Call Me.buffer.Write(.ByRef, Scan0, .Length)
106             End With
107
108             With Me.buffer
109                 Call .WriteLine(, Encoding.ASCII)
110                 Call .WriteLine("--" & boundary & "--", Encoding.ASCII)
111             End With
112         End Sub
113
114         <MethodImpl(MethodImplOptions.AggressiveInlining)>
115         Public Sub Dump(path As String)
116             Call buffer.ToArray.FlushStream(path)
117         End Sub
118
119         ''' <summary>
120         ''' POST this multipart form package to a specific web <paramref name="api"/>
121         ''' </summary>
122         ''' <param name="api$"></param>
123         ''' <param name="headers"></param>
124         ''' <returns></returns>
125         Public Function POST(api$, Optional headers As Dictionary(Of StringString) = NothingAs String
126             Dim request As HttpWebRequest = WebRequest.Create(api)
127             request.ContentType = "multipart/form-data; boundary=" & boundary
128             request.Method = "POST"
129             request.KeepAlive = True
130             request.Credentials = CredentialCache.DefaultCredentials
131
132             For Each header In headers.SafeQuery
133                 Call request.Headers.Add(header.Key, header.Value)
134             Next
135
136             Using requestStream As Stream = request.GetRequestStream
137                 buffer.Position = Scan0
138                 requestStream.Write(buffer.ToArray, Scan0, buffer.Length)
139                 requestStream.Flush()
140             End Using
141
142             Using response = request.GetResponse, responseStream As Stream = response.GetResponseStream
143                 Using responseReader As New IO.StreamReader(responseStream)
144                     Dim responseText = responseReader.ReadToEnd()
145                     Return responseText
146                 End Using
147             End Using
148         End Function
149     End Class
150 End Namespace