直接上代码:
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; } }
调用:
public void Test(string path) { System.Net.CookieContainer cc = null; using (FileStream fs = new FileStream(path, FileMode.Open)) { //缓冲区太小的话,速度慢而且伤硬盘 //声明一个4兆字节缓冲区大小,比如迅雷也有一个缓冲区,如果没有缓冲区的话, //每下载一个字节都要往磁盘进行写,非常伤磁盘,所以,先往内存的缓冲区写字节,当 //写够了一定容量之后,再往磁盘进行写操作,减低了磁盘操作。 byte[] bytes = new byte[1024*1024*5]; int readBytes; //第二个参数Offset表示当前位置的偏移量,一般都传0 int partNum = 1; while ((readBytes = fs.Read(bytes, 0, bytes.Length)) > 0) //读取的位置自动往后挪动。 { //readBytes为实际读到的byte数,因为最后一次可能不会读满。 //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 dataGet = ""; 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>(dataGet); } string dataToPostCom = "{\"access_id\":\"1230\",\"access_key\":\"abc\",\"fileName\":\"test009.mp4\"}"; string dataGetCom = ""; HttpPost.httpPost("http://xxxxxxxx/v2/DVR.UploadComplete", ref cc, ref dataToPostCom, ref dataGetCom); JavaScriptSerializer js1 = new JavaScriptSerializer(); SearchNews sn1 = js1.Deserialize<SearchNews>(dataGetCom); } }