C # Use webrequest (httprequest) to send files

Source: Internet
Author: User

Use httprequest in the C # system.net library to send a specified field of a file to the specified web server, and simulate the get part and cookies. </P> <p> using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. collections. specialized; <br/> using system. net; <br/> using system. net. sockets; <br/> using system. io; <br/> namespace completeclient <br/> {<br/> // <summary> <br/> // File Sending work class <br/> // </ summary> <br/> class postfile <br/> {<Br/> /// <summary> <br/> // send the specified field of the specified file to the specified URI through post <br/> /// </ summary> <br/> /// <Param name = "uploadfile"> Upload File Path </param> <br/> /// <Param name = "url"> upload </param> <br/> // <Param name = "offset"> current offset </param> <br/> // <Param name = "size"> size of the block to be sent </param> <br/> // <Param name = "fileformname"> file name obtained by the server "get" </Param> <br/> // <Param name = "contenttype"> file type (Reserved) </param> <br/> /// <Param n Ame = "querystring"> Get array (for the server to get some information with get) </param> <br/> // <Param name = "cookies"> Local cookies (Reserved) </param> <br/> /// <returns> the response content of the URI is returned as a string </returns> <br/> Public String uploadfileex (string uploadfile, string URL, long offset, long size, <br/> string fileformname, string contenttype, <br/> namevaluecollection querystring, cookiecontainer cookies) <br/>{< br/> If (fileformname = NULL) | <Br/> (fileformname. length = 0) <br/>{< br/> fileformname = "file "; <br/>}</P> <p> If (contenttype = NULL) ||< br/> (contenttype. length = 0) <br/>{< br/> contenttype = "application/octet-stream"; <br/>}</P> <p> string postdata; <br/> postdata = "? "; <Br/> If (querystring! = NULL) <br/>{< br/> foreach (string key in querystring. keys) <br/>{< br/> postdata + = Key + "=" + querystring. get (key) + "&"; <br/>}< br/> URI uri = new uri (URL + postdata ); </P> <p> string boundary = "----------" + datetime. now. ticks. tostring ("X"); <br/> httpwebrequest webrequest = (httpwebrequest) webrequest. create (URI); <br/> webrequest. cookiecontainer = cookies; <br/> webrequest. contenttype = "multipart/form-data; boundary =" + boundary; <br/> webrequest. method = "Post"; </P> <p> // construct the HTTP header of a POST request <br/> stringbuilder sb = new stringbuilder (); <br/> Sb. append ("--"); <br/> Sb. append (Boundary); <br/> Sb. append ("/R/N"); <br/> Sb. append ("content-Disposition: Form-data; name =/" "); <br/> Sb. append (fileformname); <br/> Sb. append ("/"; filename =/""); <br/> Sb. append (path. getfilename (uploadfile); <br/> Sb. append ("/" "); <br/> Sb. append ("/R/N"); <br/> Sb. append ("Content-Type:"); <br/> Sb. append (contenttype); <br/> Sb. append ("/R/N"); <br/> Sb. append ("/R/N"); </P> <p> string postheader = sb. tostring (); <br/> byte [] postheaderbytes = encoding. utf8.getbytes (postheader ); </P> <p> // build the trailing boundary string as a byte array <br/> // ensuring the Boundary appears on a line by itself <br/> byte [] boundarybytes = <br/> encoding. ASCII. getbytes ("/R/n --" + boundary + "/R/N"); </P> <p> filestream = new filestream (uploadfile, <br/> filemode. open, fileaccess. read); <br/> long length = postheaderbytes. length + (long) size + <br/> boundarybytes. length; <br/> webrequest. contentlength = length; </P> <p> stream requeststream = webrequest. getrequeststream (); <br/> // write the post header <br/> requeststream. write (postheaderbytes, 0, postheaderbytes. length); </P> <p> // Write File content <br/> byte [] buffer = new byte [size]; <br/> filestream. seek (offset, seekorigin. current); <br/> filestream. read (buffer, 0, buffer. length); </P> <p> requeststream. write (buffer, 0, buffer. length); </P> <p> // write the end of the post request <br/> requeststream. write (boundarybytes, 0, boundarybytes. length); <br/> // read the server feedback message <br/> webresponse Responce = webrequest. getresponse (); <br/> stream S = Responce. getresponsestream (); <br/> streamreader sr = new streamreader (s); </P> <p> return Sr. readtoend (); <br/>}< br/>

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.