Пример #1
0
        /// <summary>
        /// 发送实体到服务器并返回实体
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="url"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static T2 Post <T1, T2>(string url, T1 t)
        {
            var json = WebClientUtil.Post(url, SerializeUtil.Serialize(t));

            if (string.IsNullOrEmpty(json))
            {
                return(default(T2));
            }
            return(SerializeUtil.Deserialize <T2>(json));
        }
Пример #2
0
 /// <summary>
 /// 获取远程服务器数据
 /// </summary>
 /// <param name="url"></param>
 /// <param name="json"></param>
 /// <param name="headers"></param>
 /// <param name="timeOut"></param>
 /// <returns></returns>
 public static string Get(string url, WebHeaderCollection headers = null, int timeOut = 180 * 1000)
 {
     using (WebClientUtil client = new WebClientUtil(timeOut))
     {
         client.Encoding = System.Text.Encoding.UTF8;
         client.Headers.Add(HttpRequestHeader.Accept, "*/*");
         client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
         client.Headers.Add("Accept-Encoding", "gzip");
         client.Headers.Add("ContentEncoding", "gzip");
         client.Headers.Add("Content-Type", "application/json");
         if (headers != null)
         {
             foreach (var item in headers.AllKeys)
             {
                 client.Headers.Add(item, headers[item]);
             }
         }
         return(client.DownloadString(url));
     }
 }
Пример #3
0
 /// <summary>
 /// 上传文件
 /// </summary>
 /// <param name="url"></param>
 /// <param name="fileName"></param>
 /// <param name="headers"></param>
 /// <param name="timeOut"></param>
 /// <returns></returns>
 public static string UploadFile(string url, string fileName, WebHeaderCollection headers = null, int timeOut = 180 * 1000)
 {
     using (WebClientUtil client = new WebClientUtil(timeOut))
     {
         ServicePointManager.DefaultConnectionLimit = 512;
         client.Encoding = System.Text.Encoding.UTF8;
         client.Headers.Add(HttpRequestHeader.Accept, "*/*");
         client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
         client.Headers.Add("Accept-Encoding", "gzip");
         client.Headers.Add("ContentEncoding", "gzip");
         client.Headers.Add("Content-Type", "application/json");
         if (headers != null)
         {
             foreach (var item in headers.AllKeys)
             {
                 client.Headers.Add(item, headers[item]);
             }
         }
         return(System.Text.Encoding.UTF8.GetString(client.UploadFile(url, "POST", fileName)));
     }
 }
Пример #4
0
 /// <summary>
 /// 将实体发送给远程服务器
 /// 发送json
 /// </summary>
 /// <param name="url"></param>
 /// <param name="obj"></param>
 /// <returns></returns>
 public static string Post(string url, Object obj)
 {
     return(WebClientUtil.Post(url, SerializeUtil.Serialize(obj)));
 }