1 //调用示例 2 /// <summary> 3 /// HttpPath请求 4 /// </summary> 5 /// <param name="serviceUrl">请求地址</param> 6 /// <param name="data">发送数据</param> 7 /// <param name="statusCode">http响应状态</param> 8 /// <returns></returns> 9 public static string CommonHttpPathRequest(string serviceUrl, object data, out HttpStatusCode statusCode) 10 { 11 string ReturnString = string.Empty; 12 try 13 { 14 //构造http请求的对象 15 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl); 16 //转成网络流 17 byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(JsonConvert.SerializeObject(data)); 18 //设置 19 myRequest.Method = "Patch"; 20 myRequest.ContentLength = buf.Length; 21 myRequest.ContentType = "application/json"; 22 myRequest.MaximumAutomaticRedirections = 1; 23 myRequest.AllowAutoRedirect = true; 24 // 发送请求 25 Stream newStream = myRequest.GetRequestStream(); 26 newStream.Write(buf, 0, buf.Length); 27 newStream.Close(); 28 // 获得接口返回值 29 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); 30 statusCode = myResponse.StatusCode; 31 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 32 ReturnString = reader.ReadToEnd(); 33 reader.Close(); 34 myResponse.Close(); 35 } 36 catch (WebException e) 37 { 38 ReturnString = e.Message; 39 statusCode = HttpStatusCode.GatewayTimeout; 40 //HttpWebResponse errResponse = (HttpWebResponse)e.Response; 41 //statusCode = errResponse.StatusCode; 42 //StreamReader reader = new StreamReader(errResponse.GetResponseStream(), Encoding.UTF8); 43 //ReturnString = reader.ReadToEnd(); 44 } 45 return ReturnString; 46 }View Code
1 public static class RestClient 2 { 3 private static string BaseUri = ""; 4 5 #region Delete方式 6 public static string HttpDelete(string data, string uri) 7 { 8 return CommonHttpRequest(data, uri, "DELETE"); 9 } 10 11 public static string Delete(string uri) 12 { 13 //Web访问对象64 14 string serviceUrl = string.Format("{0}/{1}", BaseUri, uri); 15 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl); 16 myRequest.Method = "DELETE"; 17 // 获得接口返回值68 18 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); 19 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 20 //string ReturnXml = HttpUtility.UrlDecode(reader.ReadToEnd()); 21 string ReturnXml = reader.ReadToEnd(); 22 reader.Close(); 23 myResponse.Close(); 24 return ReturnXml; 25 } 26 #endregion 27 28 #region Put方式 29 public static string Put(string data, string uri) 30 { 31 return CommonHttpRequest(data, uri, "PUT"); 32 } 33 #endregion 34 35 36 #region Patch方式 37 public static string Patch(string data, string uri) 38 { 39 return CommonHttpRequest(data, uri, "Patch"); 40 } 41 #endregion 42 43 #region POST方式实现 44 45 public static string Post(string data, string uri) 46 { 47 return CommonHttpRequest(data, uri, "POST"); 48 } 49 50 public static string CommonHttpRequest(string data, string uri, string type) 51 { 52 //Web访问对象,构造请求的url地址 53 string serviceUrl = string.Format("{0}/{1}", BaseUri, uri); 54 55 //构造http请求的对象 56 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl); 57 //转成网络流 58 byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data); 59 //设置 60 myRequest.Method = type; 61 myRequest.ContentLength = buf.Length; 62 myRequest.ContentType = "application/json"; 63 myRequest.MaximumAutomaticRedirections = 1; 64 myRequest.AllowAutoRedirect = true; 65 // 发送请求 66 Stream newStream = myRequest.GetRequestStream(); 67 newStream.Write(buf, 0, buf.Length); 68 newStream.Close(); 69 // 获得接口返回值 70 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); 71 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 72 string ReturnXml = reader.ReadToEnd(); 73 reader.Close(); 74 myResponse.Close(); 75 return ReturnXml; 76 } 77 #endregion 78 79 #region GET方式实现 80 public static string Get(string uri) 81 { 82 //Web访问对象64 83 string serviceUrl = string.Format("{0}/{1}",BaseUri, uri); 84 85 //构造一个Web请求的对象 86 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl); 87 // 获得接口返回值68 88 //获取web请求的响应的内容 89 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); 90 91 //通过响应流构造一个StreamReader 92 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 93 //string ReturnXml = HttpUtility.UrlDecode(reader.ReadToEnd()); 94 string ReturnXml = reader.ReadToEnd(); 95 reader.Close(); 96 myResponse.Close(); 97 return ReturnXml; 98 } 99 #endregion 100 }View Code
标签:调用,通用,string,uri,API,myRequest,reader,data,myResponse From: https://www.cnblogs.com/qiao298/p/17310969.html