Merrion
Well-known member
I have written an app that "fills in" a web form and posts it.
This is done by encoding the parameters in multipart/form-data mode e.g.:-
Then , for example to send a usename and password to a URL you could do thus:-
The problem is that even though the form data is sent to the web service, the Page.Form property is blank at the recieving end - any ideas?
This is done by encoding the parameters in multipart/form-data mode e.g.:-
Code:
Public Class multipart_formdata
#Region "Private constants"
Private Const DEF_BOUNDARY As String = "-----------------------------7d6386366801f6"
#End Region
#Region "Private members"
Private _Boundary As String
Private _FormText As New StringBuilder
#End Region
#Region "Public interface"
Public ReadOnly Property MultipartBoundary() As String
Get
Return _Boundary
End Get
End Property
Public ReadOnly Property Text() As String
Get
Return _FormText.ToString & " " & _Boundary & "-- "
End Get
End Property
Public Sub AddParameter(ByVal ParameterName As String, ByVal ParameterValue As String)
_FormText.Append(_Boundary) ----------------------------7ce3023980c
_FormText.Append(" ")
_FormText.Append("Content-Disposition: form-data; name=")
_FormText.Append(QuoteString(ParameterName))
_FormText.Append(" ")
_FormText.Append(ParameterValue)
_FormText.Append(" ")
Content-Disposition: form-data; name="UploadFormName"; filename="C:\Directory\UploadFile.txt"
Content-Type: text/plain
This is a test file for ProjectUpload.
-----------------------------7ce3023980c--
End Sub
#End Region
#Region "Public constructor"
Public Sub New()
Me.New(DEF_BOUNDARY)
End Sub
Public Sub New(ByVal Boundary As String)
If Boundary Is Nothing OrElse Boundary = "" Then
_Boundary = DEF_BOUNDARY
Else
_Boundary = Boundary
End If
End Sub
#End Region
#Region "Private methods"
Private Function QuoteString(ByVal sIn As String) As String
Return Chr(34) & sIn & Chr(34)
End Function
#End Region
End Class
Then , for example to send a usename and password to a URL you could do thus:-
Code:
Dim webReq As HttpWebRequest = CType(WebRequest.Create("http://www.test.com"), HttpWebRequest)
Dim byteOut() As Byte
Dim _form As New multipart_formdata
With _form
.AddParameter("username", _Username)
.AddParameter("password", _Password)
End With
Dim encoder As New System.Text.UTF8Encoding
byteOut = encoder.GetBytes(_form.Text)
With webReq
.CachePolicy = New System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache)
.Accept = "*/*"
.Method = "POST" set the request method to POST
.ContentType = "multipart/form-data; boundary=" & _form.MultipartBoundary
.ContentLength = byteOut.Length - 1
.Expect = Nothing
.Referer = "http://localhost/PresentValueServlet/getValuation.htm"
End With
Dim outStream As Stream
outStream = webReq.GetRequestStream()
If outStream.CanWrite Then
\\ write the request string to it...
outStream.Write(byteOut, 0, byteOut.Length - 1)
outStream.Close()
End If
The problem is that even though the form data is sent to the web service, the Page.Form property is blank at the recieving end - any ideas?