473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 3995

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

Similar topics

5
by: Nelson Minar | last post by:
I'm writing some code to upload photos to Flickr. The Photo Upload API requires documents be POSTed via a multipart/form-data request. I was surprised to learn that Python 2.3's HTTP clients don't...
15
by: Håkan Persson | last post by:
Hi. I am trying to set up a simple HTTP-server but I have problems reading data that is beeing POSTed. class httpServer(BaseHTTPServer.BaseHTTPRequestHandler): def do_POST(self): input =...
2
by: DA | last post by:
Hello, I'm hoping someone can help me here. I've got a form that when placed on a server running HTTPS, I get an "access denied" error in IE 6 (non-SP2 and SP-2) when a user tries to upload an...
2
by: Jonathan Wax | last post by:
Hi, I spent the last week looking for an answer to the following question: How can you upload an xml file to an HTTPS server with a specific certificate. Basically doing the same as this html...
4
by: Alex Sibilev | last post by:
Hello, I have a really weird problem I've been trying to solve it without any luck for the last couple of hours :( I'm writing a "conference board" application (quite similar to ASP.NET...
2
by: shadowman | last post by:
So here's the situation: I need to write a PHP script which accepts form submissions using all methods (GET and POST) and all content types (application/x-www-form-url-encoded and...
2
by: MDANH2002 | last post by:
Hi From VB.NET I want to simulate the POST request of the following HTML form <html> <title>HTTP Post Testing</title> <body> <form action=http://www.example.com/postdata ...
3
by: JMcCrillis | last post by:
I've implemented a FileUpload servlet using AJAX and JS. It appears to be working well but for one issue. I used XMLHTTP so I could intercept the response in Javascript and write it out to a field...
0
by: JeroenToast | last post by:
I'm moving a site from a Windows machine to the online environment (Unix) both running PHP5. But have a problem to get my data properly posted on the Unix machine. On the unix machine my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.