我是新手,并且在理解如何将参数正确传递给下面代码中的Main()子时遇到一些麻烦。我之前已经通过了参数,但从未使用过数组,而我却无法找到有用的搜索结果。有人会介意我指出正确的方向吗?
链接更容易阅读。 https://github.com/freshdesk/fresh-samples/blob/master/vb.net_samples/CreateTicketWithAttachment.vb
Imports System.IO
Imports System.Net
Imports System.Text
Namespace FreshdeskTest
Class CreateTicketWithAttachment
Private Const _APIKey As String = "Api_key"
Private Const _Url As String = "http://domain.freshdesk.com/helpdesk/tickets.json"
' verify if you have to use http or https for your account
Private Shared Sub writeCRLF(o As Stream)
Dim crLf As Byte() = Encoding.ASCII.GetBytes(vbCr & vbLf)
o.Write(crLf, 0, crLf.Length)
End Sub
Private Shared Sub writeBoundaryBytes(o As Stream, b As String)
Dim d As Byte() = Encoding.ASCII.GetBytes("--" & b & vbCr & vbLf)
o.Write(d, 0, d.Length)
End Sub
Private Shared Sub writeContentDispositionFormDataHeader(o As Stream, name As String)
Dim data As String = "Content-Disposition: form-data; name=""" & name & """" & vbCr & vbLf & vbCr & vbLf
Dim b As Byte() = Encoding.ASCII.GetBytes(data)
o.Write(b, 0, b.Length)
End Sub
Private Shared Sub writeContentDispositionFileHeader(o As Stream, name As String, fileName As String, contentType As String)
Dim data As String = "Content-Disposition: form-data; name=""" & name & """; filename=""" & fileName & """" & vbCr & vbLf
data += "Content-Type: " & contentType & vbCr & vbLf & vbCr & vbLf
Dim b As Byte() = Encoding.ASCII.GetBytes(data)
o.Write(b, 0, b.Length)
End Sub
Private Shared Sub writeString(o As Stream, data As String)
Dim b As Byte() = Encoding.ASCII.GetBytes(data)
o.Write(b, 0, b.Length)
End Sub
Public Shared Sub Main(args As String())
Console.WriteLine("Application starting...")
' Define boundary:
Dim boundary As String = "----------------------------" & DateTime.Now.Ticks.ToString("x")
' Web Request:
Dim wr As HttpWebRequest = DirectCast(WebRequest.Create(_Url), HttpWebRequest)
wr.Headers.Clear()
' Method and headers:
wr.ContentType = "multipart/form-data; boundary=" & boundary
wr.Method = "POST"
wr.KeepAlive = True
' Basic auth:
Dim login As String = _APIKey & ":X"
Dim credentials As String = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(login))
wr.Headers(HttpRequestHeader.Authorization) = "Basic " & credentials
' Body:
Using rs As Stream = wr.GetRequestStream()
' Email:
writeBoundaryBytes(rs, boundary)
writeContentDispositionFormDataHeader(rs, "helpdesk_ticket[email]")
writeString(rs, "example@example.com")
writeCRLF(rs)
' Subject:
writeBoundaryBytes(rs, boundary)
writeContentDispositionFormDataHeader(rs, "helpdesk_ticket[subject]")
writeString(rs, "Ticket Title")
writeCRLF(rs)
' Description:
writeBoundaryBytes(rs, boundary)
writeContentDispositionFormDataHeader(rs, "helpdesk_ticket[description]")
writeString(rs, "Ticket description.")
writeCRLF(rs)
' Attachment:
writeBoundaryBytes(rs, boundary)
writeContentDispositionFileHeader(rs, "helpdesk_ticket[attachments][][resource]", "x.txt", "text/plain")
Dim fs As New FileStream("x.txt", FileMode.Open, FileAccess.Read)
Dim data As Byte() = New Byte(fs.Length - 1) {}
fs.Read(data, 0, data.Length)
fs.Close()
rs.Write(data, 0, data.Length)
writeCRLF(rs)
' End marker:
writeBoundaryBytes(rs, boundary)
rs.Close()
End Using
' Response processing:
Try
Console.WriteLine("Submitting Request")
Dim response = DirectCast(wr.GetResponse(), HttpWebResponse)
Dim resStream As Stream = response.GetResponseStream()
Dim resJson As String = New StreamReader(resStream, Encoding.ASCII).ReadToEnd()
Console.WriteLine(resJson)
Catch ex As Exception
Console.WriteLine("ERROR")
Console.WriteLine(ex.Message)
Finally
Console.WriteLine(Environment.NewLine)
Console.WriteLine(Environment.NewLine)
End Try
End Sub
End Class
End Namespace
答案 0 :(得分:0)
由于此代码位于类而非模块中,因此Main
不会被视为应用程序的启动点。虽然,代码很容易让人想起启动点。也许你是从某个地方复制过的?
要回答有关如何传递数组的问题,首先需要创建一个传递给它的数组。
Dim x(2) as string 'This creates 3 items in the array, 0 based
x(0) = "Test1"
x(1) = "Test2"
x(2) = "Test3"
然后,由于这是在一个类中,你必须有一个类的实例:
dim MyClass as new CreateTicketWithAttachment
然后调用sub
MyClass.Main(x)