private static ResponseResult Request( string url, string reqType, List <FormData> pars, string[] cookies = null) { #region ResponseResult resData = new ResponseResult(); HttpWebResponse httpWebResponse = null; HttpWebRequest req = null; try { string parsstring = ""; foreach (var par in pars) { parsstring += string.Format("{0}={1}&", par.key, par.value); } if (pars != null && pars.Count > 0) { parsstring = parsstring.Remove(parsstring.Length - 1); url += "?" + parsstring; } req = WebRequest.Create(url) as HttpWebRequest; req.ContentType = "application/x-www-form-urlencoded"; req.Method = reqType; if (cookies != null) { string cookiesstring = ""; foreach (var c in cookies) { cookiesstring += c + ";"; } req.Headers.Add("Cookie", cookiesstring); } try { httpWebResponse = (HttpWebResponse)req.GetResponse(); } catch (WebException ex) { httpWebResponse = (HttpWebResponse)ex.Response; } Stream responseStream = httpWebResponse.GetResponseStream(); var newcookies = httpWebResponse.Headers.GetValues("Set-Cookie"); if (newcookies != null) { resData.cookie = newcookies; } using (StreamReader resSR = new StreamReader(responseStream)) { resData.data = resSR.ReadToEnd(); resSR.Close(); responseStream.Close(); } resData.code = Convert.ToInt32(httpWebResponse.StatusCode); } catch (Exception e) { Console.WriteLine(string.Format("调用服务端接口时出错,异常说明:{0}", e)); //记录日志todo: } return(resData); #endregion }
private async static Task <ResponseResult> upload( string path, string dst, string[] cookies) { #region HttpWebResponse httpWebResponse = null; ResponseResult resData = new ResponseResult(); var url = getApiURL(Apiname_Upload); var timestamp = DateTime.Now.Ticks.ToString("x"); var boundary = "-------------------------" + timestamp; var beginBoundary = "\r\n--" + boundary + "\r\n"; var beginBoundary_withoutwrap = "--" + boundary + "\r\n"; var endBoundary = "\r\n--" + boundary + "--\r\n"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; //req.AllowWriteStreamBuffering = false; if (cookies != null) { string cookiesstring = ""; foreach (var c in cookies) { cookiesstring += c + ";"; } req.Headers.Add("Cookie", cookiesstring); } req.ContentType = "multipart/form-data;boundary=" + boundary; //组织数据流 // var memStream = new MemoryStream(); using (Stream myRequestStream = req.GetRequestStream()) { FormData filedata = new FormData { key = "file", value = path }; var part1 = getFileText(filedata, beginBoundary, endBoundary); //memStream.Write(part1, 0, part1.Length); myRequestStream.Write(part1, 0, part1.Length); //await addFileStream(memStream, path); await addFileStream(myRequestStream, path); FormData folderdata = new FormData { key = "folder", value = dst }; var part2 = getFormText(folderdata, beginBoundary, endBoundary); //memStream.Write(part2, 0, part2.Length); myRequestStream.Write(part2, 0, part2.Length); } //根据数据流建立发送的数据字节 //var tempBuffer = new byte[20*1024*1024]; //memStream.Position = 0; //int n; ////Stream myRequestStream = req.GetRequestStream(); //// memStream.Read(tempBuffer, 0, tempBuffer.Length); //while ((n = memStream.Read(tempBuffer, 0, tempBuffer.Length)) > 0) //{ // myRequestStream.Write(tempBuffer, 0, n); //} //var tempBuffer = memStream.ToArray(); // memStream.Close(); //Stream myRequestStream = req.GetRequestStream(); // myRequestStream.Write(tempBuffer, 0, tempBuffer.Length); // myRequestStream.Close(); try { httpWebResponse = (HttpWebResponse)await req.GetResponseAsync(); } catch (WebException ex) { httpWebResponse = (HttpWebResponse)ex.Response; } Stream responseStream = httpWebResponse.GetResponseStream(); using (StreamReader resSR = new StreamReader(responseStream)) { resData.data = resSR.ReadToEnd(); resSR.Close(); responseStream.Close(); } resData.code = Convert.ToInt32(httpWebResponse.StatusCode); return(resData); #endregion }