VB.Net code:
Expand|Select|Wrap|Line Numbers
- Public Shared Function Send(ByVal URL As String, _
- Optional ByVal Port As String = "", _
- Optional ByVal Servlet As String = "", _
- Optional ByVal PostString As String = "", _
- Optional ByVal SSL As Boolean = Nothing, _
- Optional ByVal faxfile As String = "", _
- Optional ByVal faxlist As String = "")
- 'https handling
- Dim oCertOverride As New CertificateOverride
- ServicePointManager.ServerCertificateValidationCallback = AddressOf oCertOverride.RemoteCertificateValidationCallback
- 'set Port number
- If Port = "" Then
- If SSL = False Then
- Port = 80
- ElseIf SSL = True Then
- Port = 443
- End If
- End If
- 'set the URL according to SSL
- If SSL = True Then
- URL = "https://" & URL & ":" & Port & Servlet
- ElseIf SSL = False Then
- URL = "http://" & URL & ":" & Port & Servlet
- Else
- Return ""
- Exit Function
- End If
- 'fax file
- Dim boundary As String = "----------------------------------------AaB03x"
- Dim contentLength
- Dim postData2 = boundary & vbCrLf & "Content-Disposition: form data; name = ""faxfile""; " & _
- "filename= """ & faxfile & """" & vbCrLf & _
- "Content-Type: image/tiff" & vbCrLf & vbCrLf
- Dim faxStream As New FileStream(faxfile, FileMode.Open, FileAccess.Read)
- Dim faxBuffer() As Byte
- ReDim faxBuffer(Math.Min(4095, faxStream.Length))
- 'fax list
- Dim postData3 = ""
- Dim listStream As FileStream = Nothing
- Dim listBuffer() As Byte = Nothing
- If faxlist <> "" Then
- postData3 = boundary & vbCrLf & "Content-Disposition: form-data; name = ""faxlist""; " & _
- "filename=""" & faxlist & """" & vbCrLf & _
- "Content-Type: text/plain" & vbCrLf & vbCrLf
- listStream = New FileStream(faxlist, FileMode.Open, FileAccess.Read)
- ReDim listBuffer(Math.Min(4095, listStream.Length))
- End If
- 'end boundary
- Dim postData4 = vbCrLf & boundary & "--"
- MsgBox(PostString & postData2 & postData3 & postData4) 'debug
- 'convert to bytes
- Dim byte1() As Byte
- Dim byte2() As Byte
- Dim byte3() As Byte = Nothing
- Dim byte4() As Byte
- byte1 = System.Text.Encoding.ASCII.GetBytes(PostString)
- byte2 = System.Text.Encoding.ASCII.GetBytes(postData2)
- byte4 = System.Text.Encoding.ASCII.GetBytes(postData4)
- 'populate content length
- contentLength = byte1.Length + byte2.Length + faxStream.Length + byte4.Length
- If faxlist <> "" Then
- byte3 = System.Text.Encoding.Default.GetBytes(postData3)
- contentLength = contentLength + byte3.Length + listStream.Length
- End If
- Dim Request As HttpWebRequest = WebRequest.Create(URL)
- Dim Response As HttpWebResponse
- Dim ResponseData As String = ""
- ' Prepare Request Object
- Request.Method = "POST"
- Request.AllowWriteStreamBuffering = False
- Request.ContentType = "multipart/form-data; boundary=" & boundary
- Request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/tiff, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*"
- Request.Headers.Add("Cache-Control", "no-cache")
- Request.UserAgent = "http generic"
- System.Net.ServicePointManager.Expect100Continue = False
- Request.ContentLength = contentLength
- MsgBox(Request.ContentLength.ToString()) 'debug
- Dim SW As Stream = Nothing
- Dim SR As StreamReader = Nothing
- ' Send Request, If Request
- Try
- SW = Request.GetRequestStream()
- If SW.CanWrite Then
- MsgBox("Writing header") 'debug
- SW.Write(byte1, 0, byte1.Length)
- MsgBox("Writing fax file") 'debug
- SW.Write(byte2, 0, byte2.Length)
- Dim bytesRead = 1
- While bytesRead <> 0
- bytesRead = faxStream.Read(faxBuffer, 0, faxBuffer.Length)
- If bytesRead <> 0 Then
- SW.Write(faxBuffer, 0, bytesRead)
- End If
- End While
- If faxlist <> "" Then
- MsgBox("Writing fax list")
- SW.Write(byte3, 0, byte3.Length)
- bytesRead = 1
- While bytesRead <> 0
- bytesRead = listStream.Read(listBuffer, 0, listBuffer.Length)
- If bytesRead <> 0 Then
- SW.Write(listBuffer, 0, bytesRead)
- End If
- End While
- End If
- MsgBox("Writing end boundary")
- SW.Write(byte4, 0, byte4.Length)
- End If
- Catch Ex As WebException
- MsgBox(" Writing Exception: " & Ex.Message)
- Catch Ex As Exception
- MsgBox("Writing Exception: " & Ex.Message)
- Return Nothing
- Exit Function
- Finally
- SW.Close()
- End Try
- ' Receive Response
- Try
- MsgBox("Receiving data") 'debug
- Response = Request.GetResponse()
- 'MsgBox("retrieving from site") 'debug
- SR = New StreamReader(Response.GetResponseStream())
- ResponseData = SR.ReadToEnd()
- MsgBox(ResponseData) 'debug
- SR.Close()
- Catch Ex As WebException
- MsgBox("Servlet Exception: " & Ex.Message)
- Catch Ex As Exception
- MsgBox("Servlet Exception: " & Ex.Message)
- Return ""
- Exit Function
- End Try
- Return ResponseData
- End Function
----------------------------------------AaB03x
Content-Disposition: form data; name="destnum"
12345678;
----------------------------------------AaB03x
Content-Disposition: form data; name="loginname"
test
----------------------------------------AaB03x
Content-Disposition: form data; name="password"
test
----------------------------------------AaB03x
Content-Disposition: form data; name="line"
1
----------------------------------------AaB03x
Content-Disposition: form data; name="faxpri"
0
----------------------------------------AaB03x
Content-Disposition: form data; name="scheduletype"
1
----------------------------------------AaB03x
Content-Disposition: form-data; name = "faxfile"; filename="C:\Users\test\Appdata\Roaming\WindowsApp lication1\Application1\1.0.0.0\Microsoft Word - Document1.TIF"
Content-Type: image/tiff
follow by file content
----------------------------------------AaB03x--
Thanks for your kind advise.