using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Net; using System.IO; using System.Text; namespace DSWeb.Areas.Mobile.DAL { public class HttpHelper { /// /// GET请求与获取结果 /// public static string HttpGet (string Url, string postDataStr="") { DBLog.Log("HttpGet"); string html = string.Empty; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url); DBLog.Log("request.Address", request.Address.ToString()); request.Method = "GET"; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream ioStream = response.GetResponseStream(); StreamReader sr = new StreamReader(ioStream, Encoding.UTF8); html = sr.ReadToEnd(); ioStream.Close(); response.Close(); DBLog.Log("html",html); return html; } /// /// POST请求与获取结果 /// public static string HttpPost ( string Url, string data ) { HttpWebRequest hwr = WebRequest.Create(Url) as HttpWebRequest; hwr.Method = "POST"; hwr.ContentType = "application/x-www-form-urlencoded"; byte[] payload; payload = System.Text.Encoding.UTF8.GetBytes(data); //通过UTF-8编码 hwr.ContentLength = payload.Length; Stream writer = hwr.GetRequestStream(); writer.Write(payload, 0, payload.Length); writer.Close(); var result = hwr.GetResponse() as HttpWebResponse; //此句是获得上面URl返回的数据 string strMsg = WebResponseGet(result); return strMsg; } public static string WebResponseGet ( HttpWebResponse webResponse ) { StreamReader responseReader = null; string responseData = ""; try { responseReader = new StreamReader(webResponse.GetResponseStream()); responseData = responseReader.ReadToEnd(); } catch { throw; } finally { webResponse.GetResponseStream().Close(); responseReader.Close(); responseReader = null; } return responseData; } } }