/// <summary> /// Post Http请求 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url">请求地址</param> /// <param name="postData">传输数据 "table=company&data=00"</param> /// <returns>泛型集合</returns> public static ResponseContent Post(string url, string postData, bool withCookes = true) { ResponseContent responseContent = new ResponseContent(); if (getLocalConnectionStatus() <= 0) { responseContent.Code = -1; responseContent.Msg = "网络未连接"; responseContent.Data = ""; return(responseContent); } if (string.IsNullOrEmpty(url)) { responseContent.Code = -1; responseContent.Msg = "请求的地址不能为空"; responseContent.Data = ""; return(responseContent); } HttpWebResponse webResponse = null; Stream responseStream = null; Stream requestStream = null; StreamReader streamReader = null; try { string respstr = PostStreamReader(url, responseStream, requestStream, streamReader, webResponse, postData, contentType); ConsoleHelper.writeLine(respstr); responseContent = JsonHelper.JsonToObject <ResponseContent>(respstr); } catch (Exception e) { ConsoleHelper.writeLine(" 请求失败: " + e.Message); responseContent.Code = -1; responseContent.Msg = "请求失败:客户端口或者服务有误。"; responseContent.Data = ""; if (e is WebException) { responseContent.Msg = e.Message; } } finally { if (responseStream != null) { responseStream.Dispose(); } if (webResponse != null) { webResponse.Dispose(); } if (requestStream != null) { requestStream.Dispose(); } if (streamReader != null) { streamReader.Dispose(); } } return(responseContent); }
/// <summary> /// 上传文件 /// </summary> /// <param name="url">地址</param> /// <param name="filePath">路径</param> /// <param name="postData">参数</param> /// <param name="contentType">媒体类型</param> /// <param name="encode">编码</param> /// <returns></returns> public static ResponseContent postFile(string url, string filePath, string postData = null, string contentType = "application/octet-stream", string encode = "UTF-8") { if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(filePath)) { throw new Exception("request url or filePath is null !"); } if (!FileHelper.Exists(filePath)) { throw new Exception("file not exists 文件不存在!"); } Stream requestStream = null; Stream responseStream = null; StreamReader streamReader = null; FileStream fileStream = null; ResponseContent response = new ResponseContent(); try { // 设置参数 HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest; //SetAuth(webRequest); webRequest.AllowAutoRedirect = true; webRequest.Method = "POST"; webRequest.CookieContainer = mCookieContainer; string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线 webRequest.ContentType = "multipart/form-data;charset=" + encode + ";boundary=" + boundary; byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); //消息开始 byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); //消息结尾 var fileName = filePath.Substring(filePath.LastIndexOf("/") + 1); //请求头部信息 string postHeader = string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:{1}\r\n\r\n", fileName, contentType); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); byte[] fileByteArr = new byte[fileStream.Length]; fileStream.Read(fileByteArr, 0, fileByteArr.Length); fileStream.Close(); requestStream = webRequest.GetRequestStream(); requestStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); requestStream.Write(fileByteArr, 0, fileByteArr.Length); if (!string.IsNullOrEmpty(postData)) { byte[] data = Encoding.UTF8.GetBytes(postData); requestStream.Write(data, 0, data.Length); } requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); requestStream.Close(); //发送请求,得到响应流 responseStream = webRequest.GetResponse().GetResponseStream(); if (responseStream == null) { response.Data = string.Empty; response.Msg = "no respose 没有返回!"; response.Code = -1; } streamReader = new StreamReader(responseStream, Encoding.UTF8); response = JsonHelper.JsonToObject <ResponseContent>(streamReader.ReadToEnd()); return(response); } catch (Exception ex) { ConsoleHelper.writeLine("请求错误:" + ex.Message); return(null); } finally { if (requestStream != null) { requestStream.Close(); } if (responseStream != null) { responseStream.Close(); } if (streamReader != null) { streamReader.Close(); } if (fileStream != null) { fileStream.Close(); } } }