コード例 #1
0
ファイル: HttpHelper.cs プロジェクト: jackjet870/api
            /// <summary>
            /// 从指定url获取数据
            /// </summary>
            /// <param name="url"></param>
            /// <returns></returns>
            public static ResponseData GetResult(string url)
            {
                try
                {
                    ResponseData   data            = new ResponseData();
                    string         responseContent = string.Empty;
                    HttpWebRequest request         = (HttpWebRequest)WebRequest.Create(url);
                    request.Timeout = 10000;
                    var response = request.GetResponse();
                    if (response == null)
                    {
                        return(data);
                    }

                    var contentType = response.ContentType.ToLower().Split(new char[] { ';' })[0];

                    //是文本类型直接返回
                    string[] contentTypes = { "application/json", "text/xml", "text/html", "text/plain" };
                    if (contentTypes.Contains(contentType))
                    {
                        using (Stream stream = response.GetResponseStream())
                        {
                            StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(REQUESTENCODING));
                            data.Content = reader.ReadToEnd();
                            reader.Dispose();
                        }

                        LoggerFactory.GetLogger().Debug(string.Format("HttpGet-{0}:{1}", url, data.Content));

                        return(data);
                    }
                    //文件类型,处理文件
                    Dictionary <string, string> dic = new Dictionary <string, string>();
                    foreach (var key in response.Headers.AllKeys)
                    {
                        string _key = key.ToLower();
                        var    k    = _key.Replace("-", "");
                        //获取文件 content-disposition,主要用户Get时获取filename
                        if (_key == "content-disposition")
                        {
                            var dispositions = response.Headers[key].Split(new char[] { ';' });
                            if (dispositions.Length > 0)
                            {
                                foreach (var disposition in dispositions)
                                {
                                    var items = disposition.Split(new char[] { '=' });
                                    if (items.Length >= 2)
                                    {
                                        dic.Add(items[0].Trim(), items[1].Replace("\"", ""));
                                    }
                                }
                            }
                            continue;
                        }
                        dic.Add(k, response.Headers[key]);
                    }

                    data.Content = JsonHelper.Encode(dic);
                    var responseStream = response.GetResponseStream();
                    var mstream        = new MemoryStream();
                    responseStream.CopyTo(mstream);
                    data.Stream = mstream;
                    responseStream.Dispose();
                    response.Close();
                    return(data);
                }
                catch (Exception ex)
                {
                    LoggerFactory.GetLogger().Error(string.Format("HttpGet发生错误-{0}", url));
                    throw ex;
                }
            }
コード例 #2
0
ファイル: HttpHelper.cs プロジェクト: jackjet870/api
            /// <summary>
            ///
            /// </summary>
            /// <param name="url"></param>
            /// <returns></returns>
            public static dynamic GetDynamicResult(string url)
            {
                var result = GetResult(url);

                return(JsonHelper.Decode <dynamic>(result.Content));
            }