1

I'm developing an Android application and sending a multipart-form-data HTTP POST to my IIS server. I need to see the raw HTTP message that my server is receiving. For this, in visual studio 2013 I put an interrupt just after this line

public String Report([Bind(Include = "lasfotos,comentario,asunto")] HttpPostedFileBase lasfotos, string comentario, string asunto)

In local variables i can see a lot of information inside this>base>request about the http message like path, content lenght, etc but i can't find where the raw HTTP is.

edit: Why do i think that see the raw http is a solution? because i can easily find my problem here i think. But i will show you my base problem: I have this function in java that send's the data to the server. i can see my variable values (in the server) of "comentario" and "asunto" but "lasfotos" = null

public static String Postear(ArrayList<File> files, String asunto, String detalle)
{
    String respuesta;
    try
    {
        String boundary = "qu1ckr3port_myb0undy";
        String filesboundary = "boundy_4_files";
        URL url = new URL("http://192.168.10.100/QuickReport/Uploads/Report");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes("--"+boundary+"\r\n" +
                "Content-Disposition: form-data; name=\"lasfotos\"\r\n" +
                "Content-Type: multipart/mixed; boundary="+filesboundary+"\r\n\r\n");
        for (byte i = 0; i < files.size(); i++)
        {
            outputStream.writeBytes("--"+filesboundary+"\r\n" +
                    "Content-Disposition: file; filename=\"" + files.get(i).getName() + "\"\r\n" +
                    "Content-Type: image/jpeg\r\n\r\n");
            FileInputStream fileInputStream = new FileInputStream(files.get(i));
            int bytesAvailable = fileInputStream.available();
            int bufferSize = Math.min(bytesAvailable, 1024 * 1024);
            byte[] buffer = new byte[bufferSize];
            int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0)
            {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, 1024 * 1024);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
            fileInputStream.close();
            outputStream.writeBytes("\r\n");
        }
        outputStream.writeBytes("--"+filesboundary+"--\r\n" +
                "--"+boundary+"\r\n" +
                "Content-Disposition: form-data; name=\"comentario\"\r\n\r\n" +
                detalle + "\r\n" +
                "--"+boundary+"\r\n");
        outputStream.writeBytes("\r\n" +
                "--"+boundary+"\r\n" +
                "Content-Disposition: form-data; name=\"asunto\"\r\n\r\n" +
                asunto + "\r\n" +
                "--"+boundary+"--\r\n");
        respuesta = "";
        String linea;
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while ((linea = reader.readLine()) != null) respuesta += linea;
        reader.close();
        outputStream.flush();
        outputStream.close();
    } catch (Exception e)
    {
        respuesta = "error";
        e.printStackTrace();
    }
    return respuesta;
}
Clamari
  • 353
  • 4
  • 17
  • `I need to see the raw HTTP message that my server is receiving.` you **need to explain why**. This looks very very much like an [XY Problem](http://meta.stackexchange.com/a/66378/171858). Instead of asking us how to get the raw http message, you should explain what having that solves. – Erik Philips May 19 '15 at 15:49
  • 2
    [Fiddler](http://www.telerik.com/fiddler) is a great tool for seeing this kind of information – Dave May 19 '15 at 15:50
  • You can get the body in visual studio and simulate the head. To get the exact byte for byte actuals, you need Fiddler. I'll see if I can look it up in my code – MatthewMartin May 19 '15 at 15:52
  • possible duplicate of [Logging raw HTTP request/response in ASP.NET MVC & IIS7](http://stackoverflow.com/questions/1038466/logging-raw-http-request-response-in-asp-net-mvc-iis7) – David W May 19 '15 at 15:53
  • `but "lasfotos" = null`. Yes. But what should it be? You are sending nothing for it. – greenapps May 19 '15 at 20:56

1 Answers1

0

the problem was bad documentation i think. i followed the format documented in this page http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

Content-Type: multipart/form-data; boundary=AaB03x

--AaB03x
Content-Disposition: form-data; name="submit-name"

Larry
--AaB03x
Content-Disposition: form-data; name="files"
Content-Type: multipart/mixed; boundary=BbC04y

--BbC04y
Content-Disposition: file; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--BbC04y
Content-Disposition: file; filename="file2.gif"
Content-Type: image/gif
Content-Transfer-Encoding: binary

...contents of file2.gif...
--BbC04y--
--AaB03x--

and that was the problem. i just changed my code and now is working perfectly. here is my new code:

public static String Postear(ArrayList<File> files, String asunto, String detalle)
{
    String respuesta;
    try
    {
        String boundary = "qu1ckr3port_myb0undy";
        URL url = new URL("http://192.168.10.100/QuickReport/Uploads/Report");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        for (byte i = 0; i < files.size(); i++)
        {
            outputStream.writeBytes("--"+boundary+"\r\n" +
                    "Content-Disposition: form-data; name=\"lasfotos\" filename=\"" + files.get(i).getName() + "\"\r\n" +
                    "Content-Type: image/jpeg\r\n\r\n");
            FileInputStream fileInputStream = new FileInputStream(files.get(i));
            int bytesAvailable = fileInputStream.available();
            int bufferSize = Math.min(bytesAvailable, 1024 * 1024);
            byte[] buffer = new byte[bufferSize];
            int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0)
            {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, 1024 * 1024);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
            fileInputStream.close();
            outputStream.writeBytes("\r\n");
        }
        outputStream.writeBytes("--"+boundary+"\r\n" +
                "Content-Disposition: form-data; name=\"comentario\"\r\n\r\n" +
                detalle + "\r\n");
        outputStream.writeBytes("\r\n" +
                "--"+boundary+"\r\n" +
                "Content-Disposition: form-data; name=\"asunto\"\r\n\r\n" +
                asunto + "\r\n" +
                "--"+boundary+"--\r\n");
        respuesta = "";
        String linea;
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while ((linea = reader.readLine()) != null) respuesta += linea;
        reader.close();
        outputStream.flush();
        outputStream.close();
    } catch (Exception e)
    {
        respuesta = "error";
        e.printStackTrace();
    }
    return respuesta;
}

hope this be usefull for someone...

Clamari
  • 353
  • 4
  • 17