static internal String HttpPut(String url, String contentType, String postData, AuthorizationParam authorization = null) { string responseData = string.Empty; try { Console.WriteLine("HTTP-PUT:" + url); byte[] byteArray = Encoding.UTF8.GetBytes(postData); //转化 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url)); request.Method = "PUT"; request.ContentType = contentType; request.ContentLength = byteArray.Length; request.Credentials = CredentialCache.DefaultCredentials; if (authorization != null) { request.Headers.Add("Authorization", authorization.ToString()); } using (Stream dataStream = request.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); } HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseData = sr.ReadToEnd(); sr.Close(); } response.Close(); } catch (WebException ex) { response = (HttpWebResponse)ex.Response; using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseData = sr.ReadToEnd(); sr.Close(); } response.Close(); throw new Exception("PUT请求失败HTTP-STATUS:" + response.StatusCode.ToString() + "错误信息:" + responseData); } } catch (Exception ex) { throw new Exception(ex.Message + "\r\nurl:" + url + "\r\nData:" + postData); } return(responseData); }
static internal String HttpDelete(string url, String contentType, AuthorizationParam authorization = null) { string responseData = string.Empty; try { Console.WriteLine("HTTP-DELETE:" + url); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url)); request.Method = "DELETE"; request.ContentType = contentType; request.Credentials = CredentialCache.DefaultCredentials; if (authorization != null) { request.Headers.Add("Authorization", authorization.ToString()); } HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseData = sr.ReadToEnd(); sr.Close(); } response.Close(); } catch (WebException ex) { response = (HttpWebResponse)ex.Response; using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseData = sr.ReadToEnd(); sr.Close(); } response.Close(); throw new Exception("DELETE请求失败HTTP-STATUS:" + response.StatusCode.ToString() + "错误信息:" + responseData); } } catch (Exception ex) { throw new Exception(ex.Message + "\r\nurl:" + url); } return(responseData); }