2

I have a HTML form and I want to simulate a request with all boxes filled out. Thanks in advance :)

<form action="upload.php" method="post" enctype="multipart/form-data"> 
        <input type="file" name="datei"><br>
        <input type="password" name="password"></br>
        <input type="submit" value="Hochladen" name="upload"> 
</form>

This is my Java code by now, but I'm only able to transfer the file, not the "password":

               FileInputStream fileInputStream = new FileInputStream(sourceFile);
               URL url = new URL("example.com/upload.php");

               conn = (HttpURLConnection) url.openConnection(); 
               conn.setDoInput(true);
               conn.setDoOutput(true);
               conn.setUseCaches(false);
               conn.setRequestMethod("POST");
               conn.setRequestProperty("ENCTYPE", "multipart/form-data");
               conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
               conn.setRequestProperty("datei", fileName);

               DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

               dos.writeBytes(twoHyphens + boundary + lineEnd); 
               dos.writeBytes("Content-Disposition: form-data; name=\"datei\"; filename=\""
                                         + fileName + "\"" + lineEnd);

               dos.writeBytes(lineEnd);

               //...

               fileInputStream.close();
               dos.flush();
               dos.close();
laalto
  • 137,703
  • 64
  • 254
  • 280
asddasasd
  • 21
  • 2

1 Answers1

1

When using multipart/form-data you should pass multiple bundles, one for each variables. However this way to push data into your request is unsafe, you should use a MultipartEntity object, take a look at this: Multipart/form-data construction with android

For multipart/form-data reference see this: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

Keep in mind that you could also post multiple variables in a single body using the "application/x-www-form-urlencoded" enctype, which is the default for html forms.

Community
  • 1
  • 1
Daniels118
  • 148
  • 9