方法一:Get请求:
/// <summary> /// httpRequest /// </summary> /// <param name="url">http接口路径</param> /// <returns>将http接口信息进行返回</returns> public string httpRequest(string Url) { string retString = string.Empty; try { LogHelper.WriteLog(GetType(), "httpRequest方法接收入参为:" + Url); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Proxy = null; request.KeepAlive = false; request.Method = "GET"; request.ContentType = "application/json; charset=UTF-8"; request.AutomaticDecompression = DecompressionMethods.GZip; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); //retString = System.Text.RegularExpressions.Regex.Unescape(retString);//字符串转义 if (response != null) { response.Close(); } if (request != null) { request.Abort(); } } catch (Exception ex) { LogHelper.WriteLog(GetType(), "httpRequest方法异常错误为:" + ex.Message); } LogHelper.WriteLog(GetType(), "httpRequest方法接收返回值为:" + retString); return retString; }
方法二Post请求:
string url = "http://192.168.1.219:8088/system-demo-newui/public/index.php/zizhu/menzhen/hospital/getAdvert";//获取http地址 string json = "{\"devCode\":\"H000_0102\"}";//获取传参json串 public void cesi() { Post(url, json); } /// <summary> /// post请求http接口 /// </summary> /// <param name="Url">http接口路径</param> /// <param name="jsonParas">传参json串</param> /// <returns></returns> public string Post(string Url, string jsonParas) { string postContent = string.Empty; HttpWebResponse response; try { LogHelper.WriteLog(GetType(), "进入Post方法,入参为:" + Url + ",入参2为:" + "jsonParas"); string strURL = Url; //创建一个HTTP请求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL); //Post请求方式 request.Method = "POST"; //内容类型 request.ContentType = "application/json"; //设置参数,并进行URL编码 string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas); byte[] payload; //将Json字符串转化为字节 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); //设置请求的ContentLength request.ContentLength = payload.Length; //发送请求,获得请求流 Stream writer; try { writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象 } catch (Exception ex) { writer = null; LogHelper.WriteLog(GetType(), "Post获取用于写入请求数据的Stream对象异常错误:" + ex.Message); // Console.Write("连接服务器失败!"); } //将请求参数写入流 writer.Write(payload, 0, payload.Length); writer.Close();//关闭请求流 // String strValue = "";//strValue为http响应所返回的字符流 try { //获得响应流 response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { response = ex.Response as HttpWebResponse; } Stream s = response.GetResponseStream(); // Stream postData = Request.InputStream; StreamReader sRead = new StreamReader(s); postContent = sRead.ReadToEnd(); sRead.Close(); } catch (Exception ex) { LogHelper.WriteLog(GetType(), "Post请求接口异常错误:" + ex.Message); } return postContent;//返回Json数据 }
标签:http,string,C#,request,接口,ex,response,请求 From: https://www.cnblogs.com/lydj/p/17030502.html