The format of the uploaded image must be specified. Otherwise, the image cannot be uploaded correctly.
During the upload, there is no blank line before the value, which leads to the upload failure and a tangle of errors.
I want to simulate a flash upload control. I started to capture packets with httpanalyze. Later, I searched the internet.[Packet capture tool] Charles v3.6.4After downloading and installing the package, you can capture the package during flash upload.
Flash upload should also be post, which may not be an HTTP packet, but it should be able to be sent in http mode. It turns out that my idea is correct and can be uploaded normally.
What is the difference between flash upload and normal button upload controls? Relevant information is to be found.
Charles v3.6.4
Original: http://www.charlesproxy.com/download/
Crack Patch: http://note.sdo.com/u/1528563206/n/r70o6 ~ Jk35opnm10i000fd
The following is a referenceCode, Pay attention to the upload format.
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. net; using system. io; namespace consoleapplication1 {class program {static void main (string [] ARGs) {dictionary <string, string> inputdic = new dictionary <string, string> (); inputdic. add ("username", "westfruit"); inputdic. add ("password", "123456"); postfile ("http://avatar.csdn.net/0/1/1/1_zfrong.jpg", "http: // W Ww.westfruit.com/news/uppic.asp? Straction = shangchuan ", @" D: \ 1.jpg", "application/octet-stream", inputdic );} # region download the file and upload it to the specified page/*** if you want to upload the file to the server on the client, we must simulate a post multipart/form-Data Type Request * Content-Type must be multipart/form-data. * The format of post requests encoded with multipart/form-data is different from that of application/X-WWW-form-urlencoded. * to use multipart/form-data, you must first set a separator in the HTTP request header, for example, 7d4a6d158c9: * we need to set the Content-Type for the simulated submission, which is different from the Content-Type for post without attachments * Here we need to: ("Content-Type ", "multipart/form-data; boundary = ABCD"); * then, separate each field with "-- 7d4a6d158c9", and the last "-- 7d4a6d158c9 --" indicates the end. * For example, to upload a title field "today" and a file c: \ 1.txt. the HTTP body is as follows: ** -- 7d4a6d158c9 * content-Disposition: Form-data; name = "title" * \ r \ n * today * -- 7d4a6d158c9 * content-Disposition: Form-data; name = "1.txt"; filename =" C: \ 1.txt" * Content-Type: text/plain * If the Image Content-Type: application/octet-stream * \ r \ n * <the content of the 1.txt File> * -- 7d4a6d158c9 * \ r \ n * Please note that, each row must end with \ r \ n followed by two \ r \ n, including the last row. * //// <Summary> /// download the file and upload it to the specified address // </Summary> /// <Param name = "geturl"> file </param> /// <Param name = "posturl"> File Upload address </param> /// <Param name = "FILENAME"> file control name </param> /// <param name = "filetype"> upload file type </param> // <Param name = "inputparamter"> other form elements </param> Public static void postfile (string geturl, string posturl, string filename, string filetype, Dictionary <string, string> inputdic) {credential Cache cache = new credentialcache (); // create a cache container cache. add (New uri ("http://www.westfruit.com/"), "Basic", new networkcredential ("westfruit", "7766517"); cookiecontainer cookies = new cookiecontainer (); // create the cookie container memorystream filestream = getfilestream (geturl ); // download the file and return the memory stream // cast the webrequest to a httpwebrequest since we're using HTTPS string boundary = "----------" + datetime. now. ticks. tostring (" X "); // The element segmentation tag httpwebrequest httpwebrequest2 = (httpwebrequest) webrequest. create (posturl); httpwebrequest2.credentials = cache; httpwebrequest2.cookiecontainer = cookies; httpwebrequest2.contenttype = "multipart/form-data; boundary =" + boundary; // boundary in other places is more than here -- httpwebrequest2.method = "Post "; // POST Request Method // build up the post message mosaic create form content stringbuilder sb = new stringbuilder (); // splice non-file Form Controls // Retrieve the health and value foreach (keyvaluepair <string, string> dicitem in inputdic) {sb. append ("--" + boundary); sb. append ("\ r \ n"); sb. append ("content-Disposition: Form-data; name = \" "+ dicitem. key + "\" "); sb. append ("\ r \ n"); sb. append ("\ r \ n"); sb. append (dicitem. value); // there must be two line breaks before the value sb. append ("\ r \ n");} // concatenate a file control sb. append ("--" + boundary); sb. append ("\ r \ n"); sb. append ("content-Disposition: Form-data; Name = \ "file1 \"; filename = \ "C :\\ upload" + path. getfilename (filename) + "\" "); sb. append ("\ r \ n"); sb. append ("Content-Type: Application/octet-stream"); sb. append ("\ r \ n"); sb. append ("\ r \ n"); // there must be two line breaks before the value. byte [] postheaderbytes = encoding. utf8.getbytes (sb. tostring (); // build the trailing boundary string as a byte array creation end flag S = encoding. ASCII. getbytes ("\ r \ n --" + boundary + "\ r \ n"); // The total HTTP request Request Length: httpwebrequest2.contentlength = postheaderbytes. length + filestream. length + boundarybytes. length; stream requeststream = httpwebrequest2.getrequeststream (); // defines an HTTP request stream // write out our post header to start marking the write stream requeststream. write (postheaderbytes, 0, postheaderbytes. length); // write out the file contents writes the attachment to the stream, up to 4 MB byte [] buff ER = 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); filestream. dispose (); // write out the trailing boundary to write the end mark to the stream requeststream. write (boundarybytes, 0, boundarybytes. length); // send http request back webresponse send http request webresponse webresponse2 = httpwebrequest2.getresponse (); // parse the HTML code from webresponse and start stream htmlstream = webresponse2.getresponsestream (); string html = gethtml (htmlstream, "UTF-8" ); Htmlstream. dispose (); // parse HTML code from webresponse to end} # endregion # region download file /// <summary> /// download file from Internet /// </Summary> /// <Param name = "geturl"> file address </param> // <returns> memory stream </returns> Public static memorystream getfilestream (string geturl) {memorystream filestream; // defines a memory stream to save the received file stream httpwebrequest httpwebrequest1 = (httpwebrequest) webrequest. create (geturl); webresponse webresponse1 = httpwebrequ Est1.getresponse (); byte [] buffer1 = new byte [4096]; // defines a 4 m number stream stream1 = webresponse1.getresponsestream (); // define a stream to accept the file stream filestream returned by html = new memorystream (); // stream1 does not support the search operation, so it is transferred to the memory stream int L; do {L = stream1.read (buffer1, 0, buffer1.length); If (L> 0) {filestream. write (buffer1, 0, L); // write the file stream to the memory stream by byte} while (L> 0); stream1.close (); filestream. position = 0; return filestream;} # endh Gion # region parse HTML code /// <summary> /// parse HTML code from the HTML stream /// </Summary> /// <Param name = "htmlstream"> HTML stream </param> /// <Param name = "encoding"> encoding format </param> /// <returns> HTML </returns> Public static string gethtml (stream htmlstream, string encoding) {streamreader objreader = new streamreader (htmlstream, system. text. encoding. getencoding (encoding); string html = ""; string Sline = ""; int I = 0; whi Le (Sline! = NULL) {I ++; Sline = objreader. Readline (); If (Sline! = NULL) HTML + = Sline;} html = html. replace ("<", "<"); html = html. replace (">", ">"); objreader. dispose (); Return HTML;} # endregion }}
reference Article : http://hi.baidu.com/westfruit/item/dba81e3b9c8b08c3382ffae8