C# 检查收到的原始HTTP消息

C# 检查收到的原始HTTP消息,c#,android,asp.net-mvc-5,C#,Android,Asp.net Mvc 5,我正在开发一个Android应用程序,并向我的IIS服务器发送一个多部分表单数据HTTP POST。我需要查看服务器正在接收的原始HTTP消息。为此,在VisualStudio2013中,我在这行后面加了一个中断 public String Report([Bind(Include = "lasfotos,comentario,asunto")] HttpPostedFileBase lasfotos, string comentario, string asunto) 在局部变量中,我可以在

我正在开发一个Android应用程序,并向我的IIS服务器发送一个多部分表单数据HTTP POST。我需要查看服务器正在接收的原始HTTP消息。为此,在VisualStudio2013中,我在这行后面加了一个中断

public String Report([Bind(Include = "lasfotos,comentario,asunto")] HttpPostedFileBase lasfotos, string comentario, string asunto)
在局部变量中,我可以在这个>base>请求中看到很多关于http消息的信息,比如路径、内容长度等,但是我找不到原始http的位置

编辑:为什么我认为查看原始http是一种解决方案?因为我可以很容易地找到我的问题。但我会告诉你我的基本问题: 我在java中有一个函数,它将数据发送到服务器。我可以看到我的变量值(在服务器中)“comentario”和“asunto”,但“lasfotos”=null

publicstaticstringpostear(arraylistfiles、stringasunto、stringdetalle)
{
字符串响应;
尝试
{
字符串边界=“qu1ckr3port\u myb000dy”;
String filesbundary=“boundy\u 4\u files”;
URL=新URL(“http://192.168.10.100/QuickReport/Uploads/Report");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(真);
connection.setUseCaches(false);
connection.setRequestMethod(“POST”);
setRequestProperty(“连接”,“保持活动”);
connection.setRequestProperty(“内容类型”、“多部分/表单数据;边界=“+boundary”);
DataOutputStream outputStream=新的DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(“--”+边界+”\r\n+
“内容处置:表单数据;名称=\“lasfotos\”\r\n”+
“内容类型:多部分/混合;边界=“+filesbundary+”\r\n\r\n”);
对于(字节i=0;i0)
{
写入(缓冲区,0,缓冲区大小);
bytesAvailable=fileInputStream.available();
bufferSize=Math.min(字节可用,1024*1024);
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
}
fileInputStream.close();
outputStream.writeBytes(“\r\n”);
}
outputStream.writeBytes(“--”+FileBoundary+”--\r\n+
“-”+边界+“\r\n”+
“内容处置:表单数据;名称=\“comentario\”\r\n\r\n”+
detalle+“\r\n”+
“-”+边界+“\r\n”);
outputStream.writeBytes(“\r\n”+
“-”+边界+“\r\n”+
“内容处置:表单数据;名称=\”asunto\“\r\n\r\n”+
asunto+“\r\n”+
“-”+边界+“-\r\n”);
respuesta=“”;
弦线;
BufferedReader=new BufferedReader(new InputStreamReader(connection.getInputStream());
而((linea=reader.readLine())!=null)respuesta+=linea;
reader.close();
outputStream.flush();
outputStream.close();
}捕获(例外e)
{
respuesta=“错误”;
e、 printStackTrace();
}
返回respuesta;
}

我认为问题在于糟糕的文档。我遵循了本页中记录的格式

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

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;
}
publicstaticstringpostear(arraylistfiles、stringasunto、stringdetalle)
{
字符串响应;
尝试
{
字符串边界=“qu1ckr3port\u myb000dy”;
URL=新URL(“http://192.168.10.100/QuickReport/Uploads/Report");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(真);
connection.setUseCaches(false);
connection.setRequestMethod(“POST”);
setRequestProperty(“连接”,“保持活动”);
connection.setRequestProperty(“内容类型”、“多部分/表单数据;边界=“+boundary”);
DataOutputStream outputStream=新的DataOutputStream(connection.getOutputStream());
对于(字节i=0;i0)
{
写入(缓冲区,0,缓冲区大小);
bytesAvailable=fileInputStream.available();
bufferSize=Math.min(字节可用,1024*1024);
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
}
fileInputStream.close();
outputStream.writeBytes(“\r\n”);
}
outputStream.writeBytes(“--”+边界+”\r\n+
“内容处置:表单数据;名称=\“comentario\”\r\n\r\n”+
detalle+“\r\n”);
outputStream.writeBytes(“\r\n”+
“-”+边界+“\r\n”+
“内容
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;
}