using System; using System.IO; using System.Net; using System.Text; namespace CRM.Core.Common { public static class Utility { public static string GetData(string Url, string RequestPara) { RequestPara = RequestPara.IndexOf('?') > -1 ? (RequestPara) : ("?" + RequestPara); WebRequest hr = HttpWebRequest.Create(Url + RequestPara); //byte[] buf = encoding.GetBytes(RequestPara); hr.Method = "GET"; System.Net.WebResponse response = hr.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string ReturnVal = reader.ReadToEnd(); reader.Close(); response.Close(); return ReturnVal; } /// /// 创建POST方式的HTTP请求 /// /// 请求的URL /// 随同请求POST的参数名称及参数值字典 /// 发送HTTP请求时所用的编码 /// public static string HttpPostData(string url, string parameters, Encoding requestEncoding) { if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } if (requestEncoding == null) { throw new ArgumentNullException("requestEncoding"); } HttpWebRequest request = null; request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; // request.ContentType = "application/json"; //如果需要POST数据 if (!string.IsNullOrEmpty(parameters)) { byte[] data = requestEncoding.GetBytes(parameters.ToString()); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { StreamReader reader = new StreamReader(response.GetResponseStream(), requestEncoding); string result = reader.ReadToEnd(); return result; } } } }