在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
不错博客: [C#HttpHelper]官方产品发布与源码下载---苏飞版 http://blog.csdn.net/xxj_jing/article/details/8107882 http://zhoufoxcn.blog.51cto.com/792419/561934/ http://www.dedecms.com/knowledge/program/asp-donet/2012/0803/4214.html http://blog.csdn.net/zhoufoxcn/article/details/6404236 C# winform post 请求指定 url /// <summary> /// 请求信息帮助 /// </summary> public class HttpHelper { /// <summary> /// post 请求指定地址返回响应数据 /// </summary> /// <param name="url">请求地址</param> /// <param name="postData">请求数据</param> /// <param name="timeout">请求超时时间(毫秒)</param> /// <param name="requestCoding">请求编码</param> /// <param name="responseCoding">响应编码</param> /// <returns></returns> public string PostRequest(string url, Dictionary<string, string> postData, int timeout, Encoding requestCoding, Encoding responseCoding) { string postStr = string.Empty; if (postData != null) { postData.All(o => { if (string.IsNullOrEmpty(postStr)) postStr = string.Format("{0}={1}", o.Key, o.Value); else postStr += string.Format("&{0}={1}", o.Key, o.Value); return true; }); } return PostRequest(url, postStr, timeout, requestCoding, responseCoding); } /// <summary> /// post 请求指定地址返回响应数据 /// </summary> /// <param name="url">请求地址</param> /// <param name="postData">请求数据</param> /// <param name="timeout">请求超时时间(毫秒)</param> /// <param name="requestCoding">请求编码</param> /// <param name="responseCoding">响应编码</param> /// <returns></returns> public string PostRequest(string url, string postData, int timeout, Encoding requestCoding, Encoding responseCoding) { string postUrl = VerifyUrl(url); try { //写入POSTDATE里 byte[] byteArray = requestCoding.GetBytes(postData); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); webRequest.Method = "POST"; webRequest.Timeout = timeout; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = byteArray.Length; Stream newStream = webRequest.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length); newStream.Close(); //接收返回信息: HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); StreamReader php = new StreamReader(response.GetResponseStream(), responseCoding); string result = php.ReadToEnd(); return result; } catch (Exception ex) { throw ex; } } /// <summary> /// 验证URL /// </summary> /// <param name="url">待验证 URL</param> /// <returns></returns> private string VerifyUrl(string url) { if (string.IsNullOrEmpty(url)) throw new Exception("URL 地址不可以为空!"); if (url.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase)) return url; return string.Format("http://{1}", url); } }
/// <summary> /// Http请求 /// </summary> public class HttpUtility { /// <summary> ///通用HttpWebRequest /// </summary> /// <param name="method">请求方式(POST/GET)</param> /// <param name="url">请求地址</param> /// <param name="param">请求参数</param> /// <param name="onComplete">完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)</param> /// <returns>请求返回的结果</returns> public static string Request(string method, string url, string param, Action<HttpStatusCode, string> onComplete = null) { if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("URL"); switch (method.ToUpper()) { case "POST": return Post(url, param, onComplete); case "GET": return Get(url, param, onComplete); default: throw new EntryPointNotFoundException("method:" + method); } } /// <summary> /// Post请求 /// </summary> /// <param name="url">请求地址</param> /// <param name="param">参数</param> /// <param name="onComplete">完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)</param> /// <returns>请求返回的结果</returns> public static string Post(string url, string param, Action<HttpStatusCode, string> onComplete = null) { UrlCheck(ref url); byte[] bufferBytes = Encoding.UTF8.GetBytes(param); var request = WebRequest.Create(url) as HttpWebRequest;//HttpWebRequest方法继承自WebRequest, Create方法在WebRequest中定义,因此此处要显示的转换 request.Method = "POST"; request.ContentLength = bufferBytes.Length; request.ContentType = "application/x-www-form-urlencoded"; try { using (var requestStream = request.GetRequestStream()) { requestStream.Write(bufferBytes, 0, bufferBytes.Length); } } catch (WebException ex) { return ex.Message; } return HttpRequest(request, onComplete); } /// <summary> /// GET请求 /// </summary> /// <param name="url">请求地址</param> /// <param name="param">参数</param> /// <param name="onComplete">完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)</param> /// <returns>请求返回结果</returns> public static string Get(string url, string param, Action<HttpStatusCode, string> onComplete = null) { UrlCheck(ref url); if (!string.IsNullOrEmpty(param)) if (!param.StartsWith("?")) param += "?" + param; else param += param; var request = WebRequest.Create(url) as HttpWebRequest; request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded"; return HttpRequest(request, onComplete); } /// <summary> /// 请求的主体部分(由此完成请求并返回请求结果) /// </summary> /// <param name="request">请求的对象</param> /// <param name="onComplete">完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)</param> /// <returns>请求返回结果</returns> private static string HttpRequest(HttpWebRequest request, Action<HttpStatusCode, string> onComplete = null) { HttpWebResponse response = null; try { response = request.GetResponse() as HttpWebResponse; } catch (WebException ex) { response = ex.Response as HttpWebResponse; } if (response == null) { if (onComplete != null) onComplete(HttpStatusCode.NotFound, "请求远程返回为空"); return null; } string result = string.Empty; using (StreamReader reader = new StreamReader(response.GetResponseStream())) { result = reader.ReadToEnd(); } if (onComplete != null) onComplete(response.StatusCode, result); return result; } /// <summary> /// URL拼写完整性检查 /// </summary> /// <param name="url">待检查的URL</param> private static void UrlCheck(ref string url) { if (!url.StartsWith("http://") && !url.StartsWith("https://")) url = "http://" + url; } }
|
请发表评论