在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
#region GET POST /// <summary> /// Get String data = GetString(URL , "PKEY=" + Pkeylist); /// </summary> /// <param name="URL"></param> /// <param name="parameters"></param> /// <returns></returns> public static string GetString(string URL, string parameters) { Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; // 准备请求... try { // 设置参数 request = WebRequest.Create(URL +"?"+ parameters) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded"; //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); string err = string.Empty; return content.Trim(); } catch (Exception ex) { string err = ex.Message; return err; } } /// <summary> /// POST string data = PostString(URL , "PKEY=" + Pkeylist); /// </summary> /// <param name="URL"></param> /// <param name="parameters"></param> /// <returns></returns> public static string PostString(string URL, string parameters) { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; byte[] data = encoding.GetBytes(parameters); // 准备请求... try { // 设置参数 request = WebRequest.Create(URL) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); string err = string.Empty; return content.Trim(); } catch (Exception ex) { string err = ex.Message; return err; } } #endregion
|
请发表评论