It is more common to use HTTP to simulate get or POST requests and submit data to the server for response. However, it is easier to use HTML form to upload files to the server, due to environment limitations, you sometimes need to use a simulated method to submit an attachment (File Upload) form. 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.
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
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 = 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 (); // Sending 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 ;}