Multipart/formdata

btbb

Member
Licensed User
Longtime User
Hi,

I'm making an app for reading and posting on an old webforum.

Now i have come to the "posting a reply" part, and i´m really stuck.

I have read Erels example on multipart/formdata request, but i don´t understand how to do that.


Can someone please help me understand how to send the multipart formdata??


//btbb


Here is the multipart/formdata from the website:

B4X:
<form action="validate.asp" method="post" enctype="multipart/form-data" name="newmessage" onSubmit="return validate()" >
  <p>
    <input type="hidden" name="messagetype" value="y">
   <input type="hidden" name="messageid" value="18969">
   <input type="hidden" name="replyid" value="117114">
  </p>
  <div align="left"></div>
  <table border="0" width="600">
    <tr> 
      <td width="100%"> <h1 align="left"> 
          Svara på inlägg
        </h1></td>
    </tr>
    <tr> 
      <td><table border="0" cellpadding="2" cellspacing="2" width="545"
  bgcolor="#FFFFFF">
                <tr> 
                  <td width="18%" align="right" valign="top" bgcolor="#EEEEFF"> 
                    <div align="left"> 
                      <script language="JavaScript1.1">
<!--
   document.newmessage.author.focus();
// -->
</script>
                      <font face="Verdana, MS Sans Serif, Arial" size="2" color="#000000"><strong><nobr>Rubrik:</nobr></strong></div></td>
                  <td width="82%" valign="top" bgcolor="#EEEEFF"> 
                    
                      <input  name="messagesubject"
      value="Styrservopump Chevrolet" size="35">
                      
                  </td>
                </tr>
                <tr> 
                  <td align="right" valign="top" bgcolor="#EEEEFF"><div align="left"><font face="Verdana, MS Sans Serif, Arial" size="2" color="#000000"><strong><nobr>Meddelande:</nobr></strong></div></td>
                  <td width="82%" valign="top" bgcolor="#EEEEFF"> 
                    <p> 
                      <textarea rows="8" name="messagetext" cols="50"></textarea>
                    </p></td>
                </tr>
                 <tr>
                  <td align="right" valign="top" bgcolor="#EEEEFF"><div align="left"><font face="Verdana, MS Sans Serif, Arial" size="2" color="#000000"><strong><nobr>Infoga bild:</nobr></strong></div></td>
                  <td valign="top" bgcolor="#EEEEFF"> 
                    <p> 
                      
                      <input type="file" name="thefile">
                      <input type="hidden" name="fname" value="20130709_130846">
                    </p></td>
                </tr>
                <tr> 
                  <td align="right" valign="top" bgcolor="#EEEEFF"><div align="left"><font face="Verdana, MS Sans Serif, Arial" size="2" color="#000000"><strong><nobr>L&auml;nk:</nobr></strong></div></td>
                  <td width="82%" valign="top" bgcolor="#EEEEFF"> 
                    <p> http:// 
                      <input name="link" type="text" value="" size="42">
                    </p></td>
                </tr>
                <tr> 
                  <td align="right" valign="top" bgcolor="#EEEEFF"><div align="left"><font face="Verdana, MS Sans Serif, Arial" size="2" color="#000000"><strong><nobr>Film </nobr></strong><nobr>(Youtube)</nobr><strong><nobr>:</nobr></strong></div></td>
                  <td width="82%" valign="top" bgcolor="#EEEEFF"> 
                    <p> 
                      <input name="film" type="text" value="" size="50">
                      <br>
                      (l&auml;nk fr&aring;n <a href="http://www.youtube.com/" target="_blank">Youtube</a>, 
                      l&auml;gg in l&auml;nken som visas under &quot;Dela&quot;) 
                    </p></td>
                </tr>

              </table>
         </td>
    </tr>
  </table>
<div align="center"><center>
    </center></div><div align="center"><center>
      <p>
        <input type="hidden" name="ip" value="11.111.111.111">
        <input type="hidden" name="ReplyParent" value="117114">
      <input type="hidden" name="intUsrID" value="2033">
        <input type="hidden" name="txtUsrName" value="btbb76">
        <input name="submit" type="submit" value="  Skicka inlägg ">
        &nbsp;&nbsp;</p>
  </center></div>
</form>
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to create a multipart request with HttpUtils2 (make sure to check Http library as well):
B4X:
Sub SendMultipartJob (JobName As String, Url As String, NameValues As Map)
   Dim boundary As String
   boundary = "---------------------------1461124740692"
   Dim stream As OutputStream
   stream.InitializeToBytesArray(20)
   Dim EOL As String
   EOL = Chr(13) & Chr(10) 'CRLF constant matches Android end of line character which is chr(10).
   Dim b() As Byte
   If NameValues <> Null AND NameValues.IsInitialized Then
      'Write the name/value pairs
      Dim key, value As String
      For i = 0 To NameValues.Size - 1
         key = NameValues.GetKeyAt(i)
         value = NameValues.GetValueAt(i)
         b = ("--" & boundary & EOL & "Content-Disposition: form-data; name=" _ 
            & QUOTE & key & QUOTE & EOL & EOL & value & EOL).GetBytes("UTF8")
         stream.WriteBytes(b, 0, b.Length)
      Next
   End If
   b = (EOL & "--" & boundary & "--" & EOL).GetBytes("UTF8")
   stream.WriteBytes(b, 0, b.Length)
   b = stream.ToBytesArray
   Dim job As HttpJob
   job.Initialize(JobName, Me)
   job.PostBytes(Url, b)
   job.GetRequest.SetContentType("multipart/form-data; boundary=" & boundary)
   job.GetRequest.SetContentEncoding("UTF8")
End Sub

You should create a map with the field names and the values and then send it to validate.asp.
 
Upvote 0

btbb

Member
Licensed User
Longtime User
Thanks for fast reply. :)

Tried this code but get "error: object not found"

B4X:
Dim values As Map
   
   values.Initialize 
   
   values.Put("messagetype","y")
   values.Put("messageid","18969")
   values.Put("replyid","117114")
   values.Put("messagesubject","Styrservopump Chevrolet")
   values.Put("messagetext","...")
   values.Put("thefile","")
   values.Put("fname","20130709_140829")
   values.Put("link","")
   values.Put("film","")
   values.Put("ip","11.111.111.111")
   values.Put("ReplyParent","117114")
   values.Put("intUsrID","2033")
   values.Put("txtUsrName","btbb76")
   values.Put("submit","  Skicka inlägg ")
   
   Dim adress As String
   adress = "http://www.example.nu/validate.asp"
      
   SendMultipartJob ("job5",adress,values)


Is this the correct way? Don´t know which object that is wrong.

/btbb
 
Last edited:
Upvote 0

btbb

Member
Licensed User
Longtime User
Seems lika i have reached some kind of firewall!

B4X:
** Service (httputils2service) Create **


** Service (httputils2service) Start **
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">



<HTML>

<HEAD>

<TITLE>WebKnight Application Firewall Alert</TITLE>

<META NAME="ROBOTS" CONTENT="NOINDEX">

</HEAD>

<BODY text="#000000" vlink="#FF3300" link="#FF3300" bgcolor="#ffffff">

<TABLE cellspacing="5" cellpadding="3" width="410">

<TR>

<TD align="left">

<FONT face="Verdana,Arial,Helvetica" size="2">

<FONT size="3"><B>WebKnight Application Firewall Alert</B></FONT><BR><BR><BR>

Your request triggered an alert! If you feel that you have received this page in error, please contact the administrator of this web site.

<BR>

<HR>

<BR><B>What is WebKnight?</B><BR>

AQTRONIX WebKnight is an application firewall for web servers and is released under the GNU General Public License. It is an ISAPI filter for securing web servers by blocking certain requests. If an alert is triggered WebKnight will take over and protect the web server.<BR><BR>

<HR>

<BR>For more information on WebKnight: <A HREF="http://www.aqtronix.com/webknight/">http://www.aqtronix.com/WebKnight/</A><BR><BR>



<B><FONT color="#FF3300">AQTRONIX</FONT> WebKnight</B></FONT>

</TD>

</TR>

</TABLE>

</BODY>

</HTML>
Error: Object Not Found
 
Upvote 0

btbb

Member
Licensed User
Longtime User
This is what the browser sends to validate.asp:

B4X:
------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="messagetype"

y
------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="messageid"

18969
------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="replyid"

117114
------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="messagesubject"

Styrservopump Chevrolet
------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="messagetext"

.
------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="thefile"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="fname"

20130709_140809
------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="link"


------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="film"


------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="ip"

11.111.111.111
------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="ReplyParent"

117114
------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="intUsrID"

2033
------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="txtUsrName"

btbb76
------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="submit"

  Skicka inlägg 
------WebKitFormBoundaryrufNvUcd5gYZwMVV--

I wonder if theres something with this line req file:

------WebKitFormBoundaryrufNvUcd5gYZwMVV
Content-Disposition: form-data; name="thefile"; filename=""
Content-Type: application/octet-stream
 
Last edited:
Upvote 0

btbb

Member
Licensed User
Longtime User
Solved that with the file. But still same response. Have added this job.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36")

but same response. :/
 
Upvote 0

btbb

Member
Licensed User
Longtime User
I have the header request from the site, but really not know how to give that header.

Here is the header: (Personal info edited)

B4X:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Cookie:__atuvc=1%7C26; ASPSESSIONIDCSSRAQQB=AAFPMGGAGJHAFNIJLOOAMIFO; examplepagesprak=txtUsrLastName=mylastname&txtUsrName=btbb76&txtUsrFirstName=myfirstname&intUsrID=2033&page=http%3A%2F%2Fwww%2Eexamplepage%2Enu%2Fmessage%2Ftreplies%2Easp%3Fmessage%3D19004%26all%3DTrue; aspmbuser=Date=2013%2D07%2D11+12%3A38%3A44&uemail=&uname=&ViewLast=ALL; __utma=206171799.1355239347.1360186811.1373392643.1373537188.116; __utmb=206171799.14.10.1373537188; __utmc=206171799; __utmz=206171799.1363860449.11.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)
Host:www.examplepage.nu
Referer:http://www.examplepage.nu/message/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36

/btbb
 
Upvote 0
Top