C # Simulate Form form submit data, upload file

Source: Internet
Author: User
Tags webp

General form data submission

            Format of data submission//Data for general form string postdata = "abc= How can I write here?" &rd= "+ DateTime.Now.Ticks;
            byte[] postd = Encoding.UTF8.GetBytes (postdata);
            String slt = "";
            HttpWebRequest request = (HttpWebRequest) webrequest.create ("Http://wechat.tsingming.test/bind/GetImage"); Request.
            method = "POST"; Request.
            ContentType = "application/x-www-form-urlencoded"; The following comments are optional//request.
            Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; Request.
            KeepAlive = true; Request. useragent = "mozilla/5.0 (Windows NT 6.1;
            
            WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/31.0.1650.57 safari/537.36 "; Stream requeststream = Request.
            GetRequestStream ();
            requestStream.Write (postd, 0, postd.length);

            Requeststream.close (); HttpWebResponse response = (HttpWebResponse) request.
      GetResponse ();      StreamReader sr = new StreamReader (response.
            GetResponseStream (), Encoding.UTF8); SLT = Sr.
            ReadToEnd ();
			
			Return Content (SLT);
			 /**************************************************************///Received method string ABC = request["abc"];

Form form for submitting files

                HttpWebRequest Webreq = (HttpWebRequest) webrequest.create ("http://localhost:8080/upload.aspx");
                Webreq.method = "POST";
                Webreq.contenttype = "application/x-www-form-urlencoded";
                webreq.accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
                Webreq.keepalive = true; Webreq.useragent = "mozilla/5.0 (Windows NT 6.1;
                
                WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/31.0.1650.57 safari/537.36 ";
                String boundary = "----" + DateTime.Now.Ticks.ToString ("x"); String formdatatemplate = "--" + Boundary + "\r\ncontent-disposition:form-data; Name=\ "{0}\";
                Filename=\ "{1}\" \r\ncontent-type:application/octet-stream\r\n\r\n "; String datatemplate = "\r\n--" + boundary + "\r\ncontent-disposition:form-data;
                Name=\ "{0}\" \r\n\r\n{1} "; var formdata = string. Format (Formdatatemplate, "Filedata", NAMe/*path.getfilename (fileName) */); Webreq.contenttype = string. Format ("Multipart/form-data;
                boundary={0} ", boundary);
                File head var formdatabyte = Encoding.ASCII.GetBytes (Formdata);

                tail var footer = Encoding.ASCII.GetBytes ("\r\n--" + boundary + "--\r\n");
                Stream requeststream = Webreq.getrequeststream (); requestStream.Write (formdatabyte, 0, formdatabyte.length)//write file header requeststream.write (streambyte, 0, Stre ambyte.length)//write file var dataByte1 = Encoding.ASCII.GetBytes (string.
                Format (DataTemplate, "type", "Memberhead")); requestStream.Write (dataByte1, 0, databyte1.length)//write parameter 1 var dataByte2 = Encoding.ASCII.GetBytes (strin
                G.format (DataTemplate, "Tid", 48650)); requestStream.Write (dataByte2, 0, databyte2.length)//write parameter 2 var dataByte3 = Encoding.ASCII.GetBytes (strin G.format (DatatEmplate, "UID", 48650)); requestStream.Write (dataByte3, 0, databyte3.length)//write parameter 3 var dataByte4 = Encoding.ASCII.GetBytes (strin
                G.format (DataTemplate, "Filename", name)); requestStream.Write (dataByte4, 0, databyte4.length)//write parameter 4 var dataByte5 = Encoding.ASCII.GetBytes (strin
                G.format (DataTemplate, "Upload", "Submit Query")); requestStream.Write (dataByte5, 0, databyte5.length)//write Parameters 5 requeststream.write (footer, 0, footer. 

                
                LENGTH);//write Tail requeststream.close ();
                HttpWebResponse response = (HttpWebResponse) webreq.getresponse (); StreamReader sr = new StreamReader (response.
                GetResponseStream (), Encoding.UTF8); Slt. Message = Sr.
                ReadToEnd (); Sr.
                Close (); Response. Close ();

Get the transferred file

HttpPostedFileBase jpeg_image_upload = request.files["Filedata"];


Note: Form format for file submission (both: Request Payload)

Content-type:multipart/form-data; boundary=----------ch2ch2gi3km7gl6ae0gi3ae0gl6gl6

Request Payload
------------ CH2CH2GI3KM7GL6AE0GI3AE0GL6GL6
content-disposition:form-data name= "Filename"

chrysanthemum.jpg
--- ---------ch2ch2gi3km7gl6ae0gi3ae0gl6gl6
content-disposition:form-data name= "UID"

48650
----------- -CH2CH2GI3KM7GL6AE0GI3AE0GL6GL6
content-disposition:form-data name= "type"

memberhead
------------ CH2CH2GI3KM7GL6AE0GI3AE0GL6GL6
content-disposition:form-data name= "Filedata" filename= "Chrysanthemum.jpg"
content-type:application/octet-stream


------------ch2ch2gi3km7gl6ae0gi3ae0gl6gl6
Content-disposition:form-data; Name= "Upload"

Submit Query
------------ch2ch2gi3km7gl6ae0gi3ae0gl6gl6--

The data format is very heavy ....






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.