POST请求,FileStream转换成Stream
/// <summary> /// POST请求 /// </summary> /// <param name="url"></param> /// <param name="payload">参数字符串</param> /// <returns></returns> public static string Post(byte[] payload) { payload = new byte[] { }; payload = QunarPolicyUtils.ReadFile("d:\\test.zip"); string url = ""; //创建post请求 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST";//请求方式post request.ContentType = "multipart/form-data;charset=utf-8";//链接类型 multipart/form-data //byte[] payload = Encoding.UTF8.GetBytes(param);//参数编码 request.ContentLength = payload.Length; //发送post的请求,写入参数 Stream writer = request.GetRequestStream(); writer.Write(payload, 0, payload.Length); writer.Close(); //接受返回来的数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream, Encoding.UTF8); string value = reader.ReadToEnd(); reader.Close(); stream.Close(); response.Close(); return value; } /// <summary> /// POST请求 /// </summary> /// <param name="payload"></param> public static string PostXML(byte[] payload) { payload = new byte[] { }; payload = QunarPolicyUtils.ReadFile("d:\\test.zip"); //发送请求 var request = (HttpWebRequest)WebRequest.Create(""); request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; var length = payload.Length; request.ContentLength = length; var writer = request.GetRequestStream(); writer.Write(payload, 0, length); writer.Close(); //接收数据 var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); return responseString; } /// <summary> /// POST请求 /// </summary> /// <param name="payload"></param> /// <returns></returns> public static string PostByte(byte[] payload) { payload = new byte[] { }; payload = QunarPolicyUtils.ReadFile("d:\\test.zip"); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(""); req.Method = "POST"; req.ContentType = "mutipart/form-data"; req.ContentLength = payload.Length; var r = req.GetRequestStream(); using (Stream reqstream = req.GetRequestStream()) { reqstream.Write(payload, 0, (int)req.ContentLength); } HttpWebResponse responstream = (HttpWebResponse)req.GetResponse(); var redstr = new StreamReader(responstream.GetResponseStream()).ReadToEnd(); return redstr; } /// <summary> /// Stream转换成FileStream /// </summary> /// <param name="stream"></param> /// <param name="fileName"></param> public static void StreamToFile(Stream stream, string fileName) { stream.Position = 0; // 把 Stream 转换成 byte[] byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); // 把 byte[] 写入文件 using (FileStream fs = new FileStream(fileName, FileMode.Create)) { using (BinaryWriter bw = new BinaryWriter(fs)) { bw.Write(bytes); bw.Close(); fs.Close(); } } } /// <summary> /// FileStream转换成Stream /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static Stream FileToStream(string fileName) { // 打开文件 using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { // 读取文件的 byte[] byte[] bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, bytes.Length); fileStream.Close(); // 把 byte[] 转换成 Stream Stream stream = new MemoryStream(bytes); return stream; } } /// <summary> /// FileStream转换成byte[] /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static byte[] ReadFile(string fileName) { using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate)) { byte[] buffer = new byte[fs.Length]; try { fs.Read(buffer, 0, buffer.Length); fs.Seek(0, SeekOrigin.Begin); return buffer; } catch { return buffer; } finally { if (fs != null) fs.Close(); } } }