From df237b08cf97c27a2526b8f523f852f298a8b9b1 Mon Sep 17 00:00:00 2001 From: lss <316519258@qq.com> Date: 星期一, 01 六月 2020 09:30:47 +0800 Subject: [PATCH] 0000 --- ZigbeeApp/Shared/Phone/UserCenter/SmartSound/Util/MyHttpWebResponse.cs | 122 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 122 insertions(+), 0 deletions(-) diff --git a/ZigbeeApp/Shared/Phone/UserCenter/SmartSound/Util/MyHttpWebResponse.cs b/ZigbeeApp/Shared/Phone/UserCenter/SmartSound/Util/MyHttpWebResponse.cs new file mode 100644 index 0000000..b5ad095 --- /dev/null +++ b/ZigbeeApp/Shared/Phone/UserCenter/SmartSound/Util/MyHttpWebResponse.cs @@ -0,0 +1,122 @@ +锘縰sing System; +using System.IO; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace Shared.Phone.UserCenter.SmartSound.Util +{ + public class MyHttpWebResponse + { + public MyHttpWebResponse() + { + } + + /// <summary> + /// Get鏁版嵁鎺ュ彛 + /// </summary> + /// <param name="getUrl">鎺ュ彛鍦板潃</param> + /// <returns></returns> + public async static Task<string> GetWebRequest(string getUrl) + { + + string responseContent = ""; + + try + { + + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getUrl); + request.ContentType = "application/json"; + request.Method = "GET"; + + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + //鍦ㄨ繖閲屽鎺ユ敹鍒扮殑椤甸潰鍐呭杩涜澶勭悊 + using (Stream resStream = response.GetResponseStream()) + { + using (StreamReader reader = new StreamReader(resStream, Encoding.UTF8)) + { + responseContent = reader.ReadToEnd().ToString(); + } + } + } + catch (Exception e) { } + return responseContent; + } + + /// <summary> + /// Post鏁版嵁鎺ュ彛 + /// </summary> + /// <param name="postUrl">鎺ュ彛鍦板潃</param> + /// <param name="paramData">鎻愪氦json鏁版嵁</param> + /// <param name="dataEncode">缂栫爜鏂瑰紡(Encoding.UTF8)</param> + /// <returns></returns> + public async static Task<string> PostWebRequest(string postUrl, string paramData, Encoding dataEncode) + { + string responseContent = string.Empty; + try + { + byte[] byteArray = dataEncode.GetBytes(paramData); //杞寲 + HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); + webReq.Method = "POST"; + webReq.ContentType = "application/x-www-form-urlencoded"; + webReq.ContentLength = byteArray.Length; + using (Stream reqStream = webReq.GetRequestStream()) + { + reqStream.Write(byteArray, 0, byteArray.Length);//鍐欏叆鍙傛暟 + //reqStream.Close(); + } + using (HttpWebResponse response = (HttpWebResponse)webReq.GetResponse()) + { + //鍦ㄨ繖閲屽鎺ユ敹鍒扮殑椤甸潰鍐呭杩涜澶勭悊 + using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default)) + { + responseContent = sr.ReadToEnd().ToString(); + } + } + } + catch (Exception ex) + { + return ex.Message; + } + return responseContent; + } + + /// <summary> + /// Put鏁版嵁鎺ュ彛 + /// </summary> + /// <param name="postUrl">鎺ュ彛鍦板潃</param> + /// <param name="paramData">鎻愪氦json鏁版嵁</param> + /// <param name="dataEncode">缂栫爜鏂瑰紡(Encoding.UTF8)</param> + /// <returns></returns> + public async static Task<string> PutWebRequest(string postUrl, string paramData, Encoding dataEncode) + { + string responseContent = string.Empty; + try + { + byte[] byteArray = dataEncode.GetBytes(paramData); //杞寲 + HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); + webReq.Method = "PUT"; + webReq.ContentType = "application/json"; + webReq.ContentLength = byteArray.Length; + using (Stream reqStream = webReq.GetRequestStream()) + { + reqStream.Write(byteArray, 0, byteArray.Length);//鍐欏叆鍙傛暟 + //reqStream.Close(); + } + using (HttpWebResponse response = (HttpWebResponse)webReq.GetResponse()) + { + //鍦ㄨ繖閲屽鎺ユ敹鍒扮殑椤甸潰鍐呭杩涜澶勭悊 + using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default)) + { + responseContent = sr.ReadToEnd().ToString(); + } + } + } + catch (Exception ex) + { + return ex.Message; + } + return responseContent; + } + } +} \ No newline at end of file -- Gitblit v1.8.0