[TestMethod] public void Test() { byte[] buffer;//文件转换为二进制流 string path = @"E:\aaa"; Encoding code = Encoding.GetEncoding("utf-8"); string[] files = Directory.GetFiles(path, "1.pdf"); //把数组转换成流中所需字节数组类型 FileStream stream = new FileInfo(files[0]).OpenRead(); buffer = new byte[stream.Length]; stream.Read(buffer, 0, Convert.ToInt32(stream.Length)); //请求远程HTTP string strResult = ""; try { //设置HttpWebRequest基本信息 HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(Api.TestUrl); myReq.Method = "post"; myReq.ContentType = "application/octet-stream"; //填充POST数据 myReq.ContentLength = buffer.Length; Stream requestStream = myReq.GetRequestStream(); requestStream.Write(buffer, 0, buffer.Length); requestStream.Close(); //发送POST数据请求服务器 HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); Stream myStream = HttpWResp.GetResponseStream(); //获取服务器返回信息 StreamReader reader = new StreamReader(myStream, code); StringBuilder responseData = new StringBuilder(); String line; while ((line = reader.ReadLine()) != null) { responseData.Append(line); } //释放 myStream.Close(); strResult = responseData.ToString(); } catch (Exception exp) { strResult = "报错:" + exp.Message; } }
这个方法还是查询出来的,以免自己下次遇到忘记,记录了一下