472,146 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Problem with http/https multipart form post using VB.Net

1
Currently I'm using VB.Net to perform a http/https multipart form post to a servlet. I'm able to perform the post using HttpWebrequest via GetRequestStream(). However, the servlet returned me with "The remote server returned an error: (500) Internal Server Error". Obviously, this means that I have not posted all the parameters as the servlet requested. But I just can't seem to find out what went wrong with my code. Hope someone can enlighten me, below is the code and sample form post for your reference:

VB.Net code:

Expand|Select|Wrap|Line Numbers
  1. Public Shared Function Send(ByVal URL As String, _
  2.         Optional ByVal Port As String = "", _
  3.         Optional ByVal Servlet As String = "", _
  4.         Optional ByVal PostString As String = "", _
  5.         Optional ByVal SSL As Boolean = Nothing, _
  6.         Optional ByVal faxfile As String = "", _
  7.         Optional ByVal faxlist As String = "")
  8.  
  9.         'https handling
  10.         Dim oCertOverride As New CertificateOverride
  11.         ServicePointManager.ServerCertificateValidationCallback = AddressOf oCertOverride.RemoteCertificateValidationCallback
  12.  
  13.         'set Port number
  14.         If Port = "" Then
  15.             If SSL = False Then
  16.                 Port = 80
  17.             ElseIf SSL = True Then
  18.                 Port = 443
  19.             End If
  20.         End If
  21.  
  22.         'set the URL according to SSL
  23.         If SSL = True Then
  24.             URL = "https://" & URL & ":" & Port & Servlet
  25.         ElseIf SSL = False Then
  26.             URL = "http://" & URL & ":" & Port & Servlet
  27.         Else
  28.             Return ""
  29.             Exit Function
  30.         End If
  31.  
  32.         'fax file
  33.         Dim boundary As String = "----------------------------------------AaB03x"
  34.         Dim contentLength
  35.         Dim postData2 = boundary & vbCrLf & "Content-Disposition: form data; name = ""faxfile""; " & _
  36.                         "filename= """ & faxfile & """" & vbCrLf & _
  37.                         "Content-Type: image/tiff" & vbCrLf & vbCrLf
  38.         Dim faxStream As New FileStream(faxfile, FileMode.Open, FileAccess.Read)
  39.         Dim faxBuffer() As Byte
  40.         ReDim faxBuffer(Math.Min(4095, faxStream.Length))
  41.  
  42.         'fax list
  43.         Dim postData3 = ""
  44.         Dim listStream As FileStream = Nothing
  45.         Dim listBuffer() As Byte = Nothing
  46.         If faxlist <> "" Then
  47.             postData3 = boundary & vbCrLf & "Content-Disposition: form-data; name = ""faxlist""; " & _
  48.                             "filename=""" & faxlist & """" & vbCrLf & _
  49.                             "Content-Type: text/plain" & vbCrLf & vbCrLf
  50.             listStream = New FileStream(faxlist, FileMode.Open, FileAccess.Read)
  51.             ReDim listBuffer(Math.Min(4095, listStream.Length))
  52.         End If
  53.         'end boundary
  54.         Dim postData4 = vbCrLf & boundary & "--"
  55.  
  56.         MsgBox(PostString & postData2 & postData3 & postData4) 'debug
  57.  
  58.         'convert to bytes
  59.         Dim byte1() As Byte
  60.         Dim byte2() As Byte
  61.         Dim byte3() As Byte = Nothing
  62.         Dim byte4() As Byte
  63.  
  64.         byte1 = System.Text.Encoding.ASCII.GetBytes(PostString)
  65.         byte2 = System.Text.Encoding.ASCII.GetBytes(postData2)
  66.         byte4 = System.Text.Encoding.ASCII.GetBytes(postData4)
  67.  
  68.         'populate content length
  69.         contentLength = byte1.Length + byte2.Length + faxStream.Length + byte4.Length
  70.  
  71.         If faxlist <> "" Then
  72.             byte3 = System.Text.Encoding.Default.GetBytes(postData3)
  73.             contentLength = contentLength + byte3.Length + listStream.Length
  74.         End If
  75.  
  76.         Dim Request As HttpWebRequest = WebRequest.Create(URL)
  77.        Dim Response As HttpWebResponse
  78.         Dim ResponseData As String = ""
  79.  
  80.         ' Prepare Request Object
  81.         Request.Method = "POST"
  82.         Request.AllowWriteStreamBuffering = False
  83.         Request.ContentType = "multipart/form-data; boundary=" & boundary
  84.         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, */*"
  85.         Request.Headers.Add("Cache-Control", "no-cache")
  86.         Request.UserAgent = "http generic"
  87.         System.Net.ServicePointManager.Expect100Continue = False
  88.         Request.ContentLength = contentLength
  89.         MsgBox(Request.ContentLength.ToString()) 'debug
  90.  
  91.         Dim SW As Stream = Nothing
  92.         Dim SR As StreamReader = Nothing
  93.  
  94.         ' Send Request, If Request
  95.         Try
  96.             SW = Request.GetRequestStream()
  97.             If SW.CanWrite Then
  98.                 MsgBox("Writing header") 'debug
  99.                 SW.Write(byte1, 0, byte1.Length)
  100.                 MsgBox("Writing fax file") 'debug
  101.                 SW.Write(byte2, 0, byte2.Length)
  102.                 Dim bytesRead = 1
  103.                 While bytesRead <> 0
  104.                     bytesRead = faxStream.Read(faxBuffer, 0, faxBuffer.Length)
  105.                     If bytesRead <> 0 Then
  106.                         SW.Write(faxBuffer, 0, bytesRead)
  107.                     End If
  108.                 End While
  109.                 If faxlist <> "" Then
  110.                     MsgBox("Writing fax list")
  111.                     SW.Write(byte3, 0, byte3.Length)
  112.                     bytesRead = 1
  113.                     While bytesRead <> 0
  114.                         bytesRead = listStream.Read(listBuffer, 0, listBuffer.Length)
  115.                         If bytesRead <> 0 Then
  116.                             SW.Write(listBuffer, 0, bytesRead)
  117.                         End If
  118.                     End While
  119.                 End If
  120.                 MsgBox("Writing end boundary")
  121.                 SW.Write(byte4, 0, byte4.Length)
  122.             End If
  123.         Catch Ex As WebException
  124.             MsgBox(" Writing Exception: " & Ex.Message)
  125.         Catch Ex As Exception
  126.             MsgBox("Writing Exception: " & Ex.Message)
  127.             Return Nothing
  128.             Exit Function
  129.         Finally
  130.             SW.Close()
  131.         End Try
  132.  
  133.         ' Receive Response
  134.         Try
  135.             MsgBox("Receiving data") 'debug
  136.             Response = Request.GetResponse()
  137.             'MsgBox("retrieving from site") 'debug
  138.             SR = New StreamReader(Response.GetResponseStream())
  139.             ResponseData = SR.ReadToEnd()
  140.             MsgBox(ResponseData) 'debug
  141.             SR.Close()
  142.         Catch Ex As WebException
  143.             MsgBox("Servlet Exception: " & Ex.Message)
  144.         Catch Ex As Exception
  145.             MsgBox("Servlet Exception: " & Ex.Message)
  146.             Return ""
  147.             Exit Function
  148.         End Try
  149.  
  150.         Return ResponseData
  151.     End Function
Sample multipart form post:

----------------------------------------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.
Nov 29 '07 #1
0 3474

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Nelson Minar | last post: by
15 posts views Thread by Håkan Persson | last post: by
2 posts views Thread by Jonathan Wax | last post: by
4 posts views Thread by Alex Sibilev | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.