检查收到的原始HTTP消息

时间:2015-05-19 15:45:14

标签: c# android asp.net-mvc-5

我正在开发Android应用程序并将多部分表单数据HTTP POST发送到我的IIS服务器。我需要查看我的服务器正在接收的原始HTTP消息。为此,在visual studio 2013中,我在此行之后放置了一个中断

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

在局部变量中,我可以在此> base>请求中查看有关http消息的大量信息,例如路径,内容长度等,但我无法找到原始HTTP的位置。

编辑:为什么我认为看到原始http是一个解决方案?因为我觉得我很容易在这里找到问题。但我会告诉你我的基本问题: 我在java中有这个函数,它将数据发送到服务器。我可以看到我的变量值(在服务器中)" comentario"和" asunto"但是" 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;
}

1 个答案:

答案 0 :(得分:0)

我认为问题是文档不好。我遵循了本页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--

这就是问题所在。我只是改变了我的代码,现在工作得很好。这是我的新代码:

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;
}

希望这对某人有用......