/// <summary> /// 请求 /// </summary> /// <param name="param"></param> /// <returns></returns> static Stream Request(HttpClientContent param) { var encoding = param.Encode; byte[] data = encoding.GetBytes(param.Data); var request = WebRequest.Create(param.Url) as HttpWebRequest; request.Method = param.MethodType.ToString(); request.ContentType = param.ContentType; if (param.Headers != null && param.Headers.Count > 0) { foreach (var item in param.Headers) { request.Headers.Add(item.Key, item.Value); } } if (param.X509Certificate != null && param.X509Certificate.Count > 0) { request.ClientCertificates.AddRange(param.X509Certificate.ToArray()); } if (param.MethodType == HttpMethodType.Post || param.MethodType == HttpMethodType.Put) { request.ContentLength = data.Length; using (var outstream = request.GetRequestStream()) { outstream.Write(data, 0, data.Length); } } var response = request.GetResponse() as HttpWebResponse; return(response.GetResponseStream()); }
/// <summary> /// Put请求 /// </summary> /// <param name="param"></param> /// <returns></returns> public static string PutString(HttpClientContent param) { try { using (var s = new StreamReader(PutRequest(param), Encoding.UTF8)) { return(s.ReadToEnd()); } } catch (Exception ex) { return(ex.Message); } }
/// <summary> /// Delete请求 /// </summary> /// <param name="param"></param> /// <returns></returns> public static Stream DeleteStream(HttpClientContent param) { return(DeleteRequest(param)); }
/// <summary> /// 异步Delete请求 /// </summary> /// <param name="param"></param> /// <returns></returns> public static async Task <byte[]> DeleteBytesAsync(HttpClientContent param) { return(await Task.Run(() => { return DeleteBytes(param); })); }
/// <summary> /// Delete请求 /// </summary> /// <param name="param"></param> /// <returns></returns> public static byte[] DeleteBytes(HttpClientContent param) { return(DeleteRequest(param).ToBytes()); }
/// <summary> /// Delete请求 /// </summary> /// <param name="param"></param> /// <returns></returns> private static Stream DeleteRequest(HttpClientContent param) { param.MethodType = HttpMethodType.Delete; return(Request(param)); }
/// <summary> /// 异步Put请求 /// </summary> /// <param name="param"></param> /// <returns></returns> public static async Task <string> PutStringAsync(HttpClientContent param) { return(await Task.Run(() => { return PutString(param); })); }
/// <summary> /// 异步Put请求 /// </summary> /// <param name="param"></param> /// <returns></returns> public static async Task <Stream> PutStreamAsync(HttpClientContent param) { return(await Task.Run(() => { return PutStream(param); })); }
/// <summary> /// Put请求 /// </summary> /// <param name="param"></param> /// <returns></returns> public static Stream PutStream(HttpClientContent param) { return(PutRequest(param)); }
/// <summary> /// Put 请求 /// </summary> /// <param name="param"></param> /// <returns></returns> public static byte[] PutBytes(HttpClientContent param) { return(PutRequest(param).ToBytes()); }
/// <summary> /// Put请求 /// </summary> /// <param name="param"></param> /// <returns></returns> public static Stream PutRequest(HttpClientContent param) { param.MethodType = HttpMethodType.Put; return(Request(param)); }
/// <summary> /// Post 请求 /// </summary> /// <param name="param"></param> /// <returns></returns> public static byte[] PostBytes(HttpClientContent param) { return(PostStream(param).ToBytes()); }