Implements the video file upload and multipart upload and file upload interfaces.

Source: Internet
Author: User

Implements the video file upload and multipart upload and file upload interfaces.

Directly run the Code:

public static bool httpPost(string url, ref CookieContainer cc, ref string dataToPost, ref string dataget, bool isChangeCookie = false)        {            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);            request.AllowAutoRedirect = false;            request.KeepAlive = true;            request.Method = "POST";            request.ProtocolVersion = HttpVersion.Version11;            request.Proxy = WebRequest.DefaultWebProxy;            request.Credentials = CredentialCache.DefaultCredentials;            request.Headers.Add("Cache-Control: max-age=0");            request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4,zh-TW;q=0.2,fr;q=0.2,ja;q=0.2");            request.ContentType = "application/x-www-form-urlencoded";            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";            request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2008.2 Safari/537.36";            request.CookieContainer = cc;            try            {                StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);                writer.Write((string)dataToPost);                writer.Flush();                HttpWebResponse response = (HttpWebResponse)request.GetResponse();                if (response.ContentEncoding == "gzip")                {                    MemoryStream ms = new MemoryStream();                    GZipStream zip = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);                    byte[] buffer = new byte[1024];                    int l = zip.Read(buffer, 0, buffer.Length);                    while (l > 0)                    {                        ms.Write(buffer, 0, l);                        l = zip.Read(buffer, 0, buffer.Length);                    }                    ms.Dispose();                    zip.Dispose();                    dataget = Encoding.UTF8.GetString(ms.ToArray());                }                else                    dataget = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")).ReadToEnd();                response.Close();                request.Abort();                return true;            }            catch (WebException exception)            {                if (exception.Response != null)                {                    dataget = ((HttpWebResponse)exception.Response).StatusCode.ToString();                }                request.Abort();                return false;            }            catch            {                dataget = "error";                request.Abort();                return false;            }        }    }    public class SearchNews    {        public string location { get; set; }        public string fileName { get; set; }        public string FlagString { get; set; }        public string Flag { get; set; }    }

Call:

Public void Test (string path) {System. net. cookieContainer cc = null; using (FileStream fs = new FileStream (path, FileMode. (Open) {// If the buffer zone is too small, the speed will be slow and the hard disk will be damaged. // declare a 4 MB buffer zone. For example, thunder also has a buffer zone. If there is no buffer zone, // each downloaded byte must be written to the disk, which seriously impairs the disk. Therefore, write the byte to the memory buffer first. When the capacity is sufficient, write Data to the disk to reduce disk operations. Byte [] bytes = new byte [1024*1024*5]; int readBytes; // The second parameter Offset indicates the Offset of the current position. Generally, 0 int partNum = 1 is passed; while (readBytes = fs. read (bytes, 0, bytes. length)> 0) // The read position is automatically moved back. {// ReadBytes indicates the number of actually read bytes, because the last read may not be full. // OutStream. write (bytes, 0, readBytes); // byte [] fileData = new byte [outStream. length]; byte [] buffer = bytes. take (readBytes ). toArray (); // fs. read (buffer, 0, readBytes); string part = HttpUtility. urlEncode (Convert. toBase64String (buffer); string shortet = ""; string dataToPost = "{\" access_id \ ": \" 1230 \ ", \" access_key \": \ "abc \", \ "fileName \": \ "test009.mp4 \", \ "part \": \ "" + part + "\", \ "partNum \": "+ partNum +"} "; HttpPost. httpPost (" http://xxxxxx/v2/DVR.UploadPart ", Ref cc, ref dataToPost, ref dataGet); partNum ++; JavaScriptSerializer js = new JavaScriptSerializer (); SearchNews sn = js. deserialize <SearchNews> (shortet);} string dataToPostCom = "{\" access_id \ ": \" 1230 \ ", \" access_key \ ": \" abc \", \ "fileName \": \ "test009.mp4 \"} "; string paietcom =" "; HttpPost. httpPost (" http://xxxxxxxx/v2/DVR.UploadComplete ", Ref cc, ref dataToPostCom, ref dataGetCom); JavaScriptSerializer js1 = new JavaScriptSerializer (); SearchNews sn1 = js1.Deserialize <SearchNews> (dataGetCom );}}

 

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.