//====================================================================== // HttpHelper // ZMF_Demo.ZMF // // Created by Chen Jialuo on 3/2/2017 ‏‎4:21:55 PM. // Copyright © 2017 ZHONGMAKEJI. All rights reserved. // http://www.zhongmakj.com/ // //====================================================================== using System.Collections; using System.Collections.Specialized; using System.Net; namespace MobileServicePlatform.Common { /// /// helper for http get/post /// internal sealed class HttpHelper { /// /// 不需要新建对象,构造函数为私有 /// private HttpHelper() { ; } /// /// 发送HTTP POST /// /// 目标完整URL /// FORM内容 /// 调用成功则返回接口返回内容;失败时返回null public static string Post(string url, NameValueCollection param) { try { using (var client = new WebClient()) { byte[] result = client.UploadValues(url, "POST", param); return System.Text.Encoding.Default.GetString(result); } } catch { // TODO: HTTP POST请求发生异常,错误日志写入 return null; } } public static string Post(string url, Hashtable param) { NameValueCollection nvc = new NameValueCollection(); foreach (DictionaryEntry entry in param) { nvc.Add(entry.Key.ToString(), entry.Value.ToString()); } return Post(url,nvc); } } }