Let's not say how to simulate the data, and look through a simple form to see what data the client submits to the server when the request occurs.
Here is a simple HTML form, two text input boxes, a file upload (here I choose a picture), note that there is a file upload form of the enctype attribute.
Copy Code code as follows:
<form action= "sql.aspx" method= "post" enctype= "Multipart/form-data" >
<input id= "Text1" name= "Content" type= "text"/><br/>
<input id= "Text2" name= "uploadimg" type= "text"/><br/>
<input id= "File1" type= "file" Name= "Image0"/><br/>
<input id= "Submit1" type= "Submit" value= "Submit"/>
</form>
To view the form submission, what data was posted to the service side, which I use Fiddler to view. Fiddler is really a good tool, note that when the URL host address is localhost fiddler capture, need to localhost after adding a bit (.) You can open the Fiddler, browse the page with the above form, enter the data submission, and then see the post data in the fiddler. Here is a screenshot of the data.
Analysis of the data is not difficult to draw, a form of data fields (input type= "text") corresponds to the format of
-----------------------------7DA119C1004A6
Content-disposition:form-data; Name= "Content"
This is a TXT value
A file (input type= "file") corresponds to the format (usually the last parameter of the form)
-----------------------------7DA119C1004A6
Content-disposition:form-data; Name= "IMAGE0"; Filename= "E:\CAI\875.jpg"
Content-type:image/pjpeg
[File contents]
-----------------------------7da119c1004a6--at the end.
With the above data for reference, organize the data according to its format, post to the server, can also achieve the effect of HTML form submission. To pay special attention to its format: such as carriage return line, the poor one may not get the correct response, as well as the requested content-length a certain calculation. Here is a reference:
Code
Copy Code code as follows:
public string Postfile (String v1,string v2, string file)
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString ("x");
Request
WebRequest req = webrequest.create (@ "http://localhost.:4944/WebSite1/getfile.aspx");
Req. method = "POST";
Req. ContentType = "Multipart/form-data; boundary= "+ boundary;
Organize form data
StringBuilder sb = new StringBuilder ();
Sb. Append ("--" + boundary);
Sb. Append ("\ r \ n");
Sb. Append ("Content-disposition:form-data; Name=\ "content\");
Sb. Append ("\r\n\r\n");
Sb. Append (v1);
Sb. Append ("\ r \ n");
Sb. Append ("--" + boundary);
Sb. Append ("\ r \ n");
Sb. Append ("Content-disposition:form-data; Name=\ "uploadimg\");
Sb. Append ("\r\n\r\n");
Sb. Append ("v2");
Sb. Append ("\ r \ n");
Sb. Append ("--" + boundary);
Sb. Append ("\ r \ n");
Sb. Append ("Content-disposition:form-data; Name=\ "Image0\"; Filename=\ "e:\\a.jpg\");
Sb. Append ("\ r \ n");
Sb. Append ("Content-type:image/pjpeg");
Sb. Append ("\r\n\r\n");
String head = sb. ToString ();
byte[] Form_data = Encoding.UTF8.GetBytes (head);
End
byte[] Foot_data = Encoding.UTF8.GetBytes ("\r\n--" + boundary + "--\r\n");
File
FileStream FileStream = new FileStream (file, FileMode.Open, FileAccess.Read);
Post Total length
Long length = Form_data. Length + filestream.length + foot_data. Length;
Req. ContentLength = length;
Stream Requeststream = req. GetRequestStream ();
Send form parameters
requestStream.Write (form_data, 0, Form_data. Length);
File contents
byte[] buffer = new byte[checked ((UINT) math.min (4096, (int) filestream.length))];
int bytesread = 0;
while (bytesread = filestream.read (buffer, 0, buffer.) Length))!= 0)
requestStream.Write (buffer, 0, bytesread);
End
requestStream.Write (foot_data, 0, Foot_data. Length);
Requeststream.close ();
Response
WebResponse pos = req. GetResponse ();
StreamReader sr = new StreamReader (pos. GetResponseStream (), Encoding.UTF8);
String html = Sr. ReadToEnd (). Trim ();
Sr. Close ();
if (pos!= null)
{
Pos. Close ();
pos = null;
}
if (req!= null)
{
req = null;
}
return HTML;
}