We will not talk about how to simulate data for the moment. We can use a simple form to see what data the client has submitted to the server when a request occurs.
The following is a simple html form, two text input boxes, and one file upload (here I select an image). Note that the enctype attribute of the form to be uploaded is available.
Copy codeThe Code is 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 what data is posted to the server when a form is submitted, I use Fiddler to view the data. Fiddler is indeed a good tool. Note that when the url host address is localhost, Fiddler cannot be captured. Add a bit (.) After localhost (.) you can open Fiddler, browse the page with the form above, and submit the input data. In Fiddler, you can see the post data. Below is a copy of the data.
It is not difficult to analyze the data. The format of the data field (input type = "text") in a form is
----------------------------- 7da119c1004a6
Content-Disposition: form-data; name = "content"
This is a txt value
The format of a file (input type = "file") is (usually the last parameter of the form)
----------------------------- 7da119c1004a6
Content-Disposition: form-data; name = "image0"; filename = "E: \ CAI \ 875.jpg"
Content-Type: image/pjpeg
[File content]
The end is ----------------------------- 7da119c1004a6 --
With the above data for reference, organize the data according to its format and post it to the server, which can also achieve the html form submission effect. Pay special attention to the format: for example, if you press enter to wrap a line, the difference may not be able to get the correct response, and the request Content-Length must be calculated. The following is a reference:
Code
Copy codeThe Code is 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 ");
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 ");
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 ");
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 );
// Total post 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 Content
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;
}