2015-05-19 72 views
1

我正在开发一个Android应用程序并向我的IIS服务器发送multipart-form-data HTTP POST。我需要看到我的服务器正在接收的原始HTTP消息。为此,在Visual Studio 2013,我把一个中断只是这一行检查收到的原始HTTP消息

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

在局部变量,我可以看到很多有关类似路径的HTTP消息,内容lenght等,但这一>基地>要求后里面我无法找到原始HTTP的位置。

编辑:我为什么认为看到原始http是一个解决方案?因为我可以很容易地在这里找到我的问题。但我会告诉你我的基本问题: 我有这个功能在Java中发送的数据到服务器。我可以看到“comentario”和“asunto”,而是“lasfotos” =空

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

'我需要看到的是我的服务器receiving.'你**需要解释为什么原始的HTTP消息** 。这看起来非常像[XY问题](http://meta.stackexchange.com/a/66378/171858)。不要问我们如何获得原始的http消息,你应该解释什么才能解决。 –

+2

[Fiddler](http://www.telerik.com/fiddler)是查看此类信息的好工具 – dave

+0

您可以在Visual Studio中获取身体并模拟头部。要获得字节实际的确切字节,您需要Fiddler。我会看看我是否可以在我的代码 – MatthewMartin

回答

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

希望这是有用的人......