| | |
| | | /// <returns></returns> |
| | | public static async System.Threading.Tasks.Task<string> HttpWebRequest(string getUrl, string str, string method, int second = 3, bool _bool = false) |
| | | { |
| | | HttpWebRequest request = WebRequest.Create(getUrl) as HttpWebRequest; //创建请求 |
| | | request.Method = method; //请求方式为POST/GET |
| | | request.ContentType = "application/json"; |
| | | request.Timeout = second * 1000;//超时时间 |
| | | if (_bool) { |
| | | //用于高胜可视对讲接口 |
| | | request.Headers.Add("Authorization", Config.Instance.Token); |
| | | try
|
| | | {
|
| | | HttpWebRequest request = WebRequest.Create(getUrl) as HttpWebRequest; //创建请求
|
| | | request.Method = method; //请求方式为POST/GET
|
| | | request.ContentType = "application/json";
|
| | | request.Timeout = second * 1000;//超时时间
|
| | | if (_bool)
|
| | | {
|
| | | //用于高胜可视对讲接口
|
| | | request.Headers.Add("Authorization", Config.Instance.Token);
|
| | | }
|
| | | if (method == "POST")
|
| | | {
|
| | | byte[] jsonbyte = System.Text.Encoding.UTF8.GetBytes(str);
|
| | | request.ContentLength = jsonbyte.Length;
|
| | | Stream postStream = request.GetRequestStream();
|
| | | postStream.Write(jsonbyte, 0, jsonbyte.Length);
|
| | | postStream.Close();
|
| | | }
|
| | | //发送请求并获取相应回应数据 |
| | | HttpWebResponse res;
|
| | | try
|
| | | {
|
| | | res = (HttpWebResponse)request.GetResponse();
|
| | | }
|
| | | catch (WebException ex)
|
| | | {
|
| | | res = (HttpWebResponse)ex.Response;
|
| | | }
|
| | | StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
|
| | | string content = sr.ReadToEnd(); //获得响应字符串
|
| | | return content;
|
| | | } |
| | | if (method == "POST") |
| | | { |
| | | byte[] jsonbyte = System.Text.Encoding.UTF8.GetBytes(str); |
| | | request.ContentLength = jsonbyte.Length; |
| | | Stream postStream = request.GetRequestStream(); |
| | | postStream.Write(jsonbyte, 0, jsonbyte.Length); |
| | | postStream.Close(); |
| | | catch |
| | | {
|
| | | return null;
|
| | | } |
| | | //发送请求并获取相应回应数据 |
| | | HttpWebResponse res; |
| | | try |
| | | { |
| | | res = (HttpWebResponse)request.GetResponse(); |
| | | } |
| | | catch (WebException ex) |
| | | { |
| | | res = (HttpWebResponse)ex.Response; |
| | | } |
| | | StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8); |
| | | string content = sr.ReadToEnd(); //获得响应字符串 |
| | | return content; |
| | | } |
| | | /// <summary> |
| | | /// 请求服务器的方法 |