async Task <string> upload_firstChunk(string uuid, PartViewStream pvs, long iblock, string uploadToken, string host, string keyname) { var uploadUrl = host + $"/mkblk/{block_size}/{iblock}"; HttpWebRequest req = WebRequest.CreateHttp(uploadUrl); req.Method = "POST"; req.Headers.Add("Authorization", uploadToken); req.Headers.Add("UploadBatch", uuid); req.ContentLength = pvs.Length; req.ContentType = "application/octet-stream"; var reqStream = await req.GetRequestStreamAsync(); pvs.BlockCopyTo(reqStream); reqStream.Close(); var resp = (HttpWebResponse)await req.GetResponseAsync(); var respStream = resp.GetResponseStream(); var sr = new StreamReader(respStream); var retjson = sr.ReadToEnd(); sr.Close(); resp.Close(); req.Abort(); Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(retjson); if (!json.ContainsKey("ctx")) { return(null); } return(json["ctx"].ToString()); }
private async Task <string> upload_restChunk(string uuid, PartViewStream pvs, string ctx, string uploadToken, string host, int ichunk, string keyname) { ///POST /bput/<ctx>/<nextChunkOffset> ///Host: <UploadDomain> ///Authorization:<UploadToken> ///Content-Length:<ChunkSize> ///Content-Type:application/octet-stream ///UploadBatch:<uuid> ///Key:<key> /// ///<ChunkBinary> var uploadUrl = host + $"/bput/{ctx}/{ichunk * chunk_size}"; HttpWebRequest req = WebRequest.CreateHttp(uploadUrl); req.Method = "POST"; //req.Headers.Add("Qingzhen-Token", recentToken); req.ContentLength = pvs.Length; req.Headers.Add("Authorization", uploadToken); req.ContentType = "application/octet-stream"; req.Headers.Add("UploadBatch", uuid); //req.Headers.Add("Key", UrlSafeBase64.encode(keyname)); var reqStream = await req.GetRequestStreamAsync(); //await pvs.CopyToAsync(reqStream); // CopyTo(reqSteam) 使用在这个请求里可能发生异常,故用自己写的BlockCopyTo pvs.BlockCopyTo(reqStream); reqStream.Close(); HttpWebResponse resp = (HttpWebResponse)await req.GetResponseAsync(); var respStream = resp.GetResponseStream(); var sr = new StreamReader(respStream); var retjson = await sr.ReadToEndAsync(); sr.Close(); resp.Close(); req.Abort(); Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(retjson); if (!json.ContainsKey("ctx")) { return(null); } return(json["ctx"].ToString()); }