C # Impersonation submits a form with file upload

Source: Internet
Author: User
Tags diff

------Webkitformboundary8gsfyexbioo5oyaacontent-disposition:form-data; Name= "username" admin------webkitformboundary8gsfyexbioo5oyaacontent-disposition:form-data; Name= "Submit" login------webkitformboundary8gsfyexbioo5oyaacontent-disposition:form-data; Name= "Max_file_size"; Filename= "2aêo1üàí.txt" content-type:text/plain//unrecognized file format, such as RAR for Content-type:application/octet-stream. PDF is content-type:application/pdf------webkitformboundary8gsfyexbioo5oyaa--
The request data stream format is:------fixed boundary character//1, the boundary character is consistent with Content-type:multipart/form-data in the request header; boundary=----Webkitformboundaryxxbqdgnaymzzuaxzcontent-disposition:form-data; Name= "username"//2, content description and content name//2.1, if uploading the file you need to fill in the file type Content-type:text/plain//3, empty line//4, string or file binary//If there are other data to be submitted Repeat 1-4------fixed boundary characters//5, end of boundary should be added at the end

C # analog upload file code

Public postfile (String action,string file)  {             //1, data boundaries              string boundary =  "---------------------------"  + datetime.now.ticks.tostring ("X");             //2, creating HttpWebRequest requests              HttpWebRequest myRequest =  WebRequest.Create (Action)  as HttpWebRequest;             myRequest.Method =  "POST";             //3, set request contenttype  and   boundary characters (the boundary character must be the same as the boundary character of the request data body   Otherwise the server cannot resolve)              myRequest.ContentType =  "Multipart/form-data; Boundary= " + boundary;&nbSP;           //4, adding file Data description information              stringbuilder sb = new stringbuilder ();             SB. Append ("--"  + boundary);             sb. Append ("\ r \ n");            //name  for   Upload the file INPUT NAME            SB. Append ("Content-disposition: form-data; name= ' max_file_size ';  filename=\" " + file  +  "\" ");             sb. Append ("\ r \ n");             sb. Append ("Content-type: application/octet-stream");  //here is a simulated file type, in which case the browser will actually determine this type based on the local file suffix name   & NBSP;         SB. Append ("\r\n\r\n");            encoding  Encoding = encoding.getencoding ("GBK");  //here encoding must be encoded with the Web page   Otherwise cause the Chinese path or file name garbled   But the contents of the file will not be garbled              byte[] form_data = encoding. GetBytes (sb.) ToString ());             //5, footer Data              byte[] foot_data = encoding. GetBytes ("\r\n--"  + boundary +  "--\r\n");                         //6, read files              using  (filestream filestream  = new filestream (file, filemode.open, fileaccesS.read))             {                 stringbuilder sb2 = new  stringbuilder ();                 SB2. Append ("--"  + boundary);                 SB2. Append ("\ r \ n");                 SB2. Append ("content-disposition: form-data; name= ' username '; \ r \ n");  //sent content title                  SB2. Append ("\ r \ n");                 SB2. Append ("Chinese name");  //send content              & nbsp;   sb2. Append ("\ r \ n")  //each set of data ends with a newline character to be added                  SB2. Append ("--"  + boundary);                 SB2. Append ("\ r \ n");                 SB2. Append ("Content-disposition: form-data; name= ' pwd '; \ r \ n");                 SB2. Append ("\ r \ n");                 SB2. Append ("QADSFDSFA");                 SB2. Append ("\ r \ n");                 byte[] data = encoding. GetBytes (SB2. ToString ());                //6, set upload data length to header  +  file  +  Footer Length                  myrequest.contentlength = form_data. Length + foot_data. Length + filestream.length + data. LENGTH;                //7, Get the requested data stream                 stream  requeststream = myrequest.getrequeststream ();                 //8.1, writing character information data to request flow                  requeststream.write (Data, 0, data. Length);                 //8.2, Write the file information dataIncoming Request Flow                  requestStream.Write (Form_data, 0, form_data. Length);                 //9, Loop read file stream   Write request flow                  byte[] buffer = new byte[checked (UINT) math.min (4096,  (int) fileStream.Length))] ;                int  bytesread = 0;                 while  (Bytesread = filestream.read (Buffer, 0, buffer. Length))  != 0)                  {               &nbSp;    requeststream.write (Buffer, 0, bytesread);                 }                                  //10, writes end boundary data to the request stream                  requeststream.write (Foot_data, 0, foot_data. Length);            }             //11, initiating a request              httpwebresponse myresponse = myrequest.getresponse ()  as  HTTPWEBRESPONSE;            //12, read request return Data flow               streamreader sr = new streamreader (MyResponse.GetResponseStream () ,  encoding);             string json  = SR. ReadToEnd (). Trim ();             clipboard.settext (JSON);             messagebox.show (JSON);                     }


C # Impersonation submits a form with file upload

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.