Esempio n. 1
0
 /// <summary>
 /// 根据相传入的数据,得到相应页面数据
 /// </summary>
 /// <param name="item">参数类对象</param>
 /// <returns>返回HttpResult类型</returns>
 public HttpResult GetHtml(HttpItem item)
 {
     //返回参数
     HttpResult result = new HttpResult();
     try
     {
         //准备参数
         SetRequest(item);
     }
     catch (Exception ex)
     {
         result.Cookie = string.Empty;
         result.Header = null;
         result.Html = ex.Message;
         result.StatusDescription = "配置参数时出错:" + ex.Message;
         //配置参数时出错
         return result;
     }
     try
     {
         //请求数据
         using (response = (HttpWebResponse)request.GetResponse())
         {
             GetData(item, result);
         }
     }
     catch (WebException ex)
     {
         if (ex.Response != null)
         {
             using (response = (HttpWebResponse)ex.Response)
             {
                 GetData(item, result);
             }
         }
         else
         {
             result.Html = ex.Message;
         }
     }
     catch (Exception ex)
     {
         result.Html = ex.Message;
     }
     if (item.IsToLower) result.Html = result.Html.ToLower();
     return result;
 }
Esempio n. 2
0
 /// <summary>
 /// 设置编码
 /// </summary>
 /// <param name="item">HttpItem</param>
 /// <param name="result">HttpResult</param>
 /// <param name="ResponseByte">byte[]</param>
 private void SetEncoding(HttpItem item, HttpResult result, byte[] ResponseByte)
 {
     //是否返回Byte类型数据
     if (item.ResultType == ResultType.Byte) result.ResultByte = ResponseByte;
     //从这里开始我们要无视编码了
     if (encoding == null)
     {
         Match meta = Regex.Match(Encoding.Default.GetString(ResponseByte), "<meta[^<]*charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
         string c = string.Empty;
         if (meta != null && meta.Groups.Count > 0)
         {
             c = meta.Groups[1].Value.ToLower().Trim();
         }
         if (c.Length > 2)
         {
             try
             {
                 encoding = Encoding.GetEncoding(c.Replace("\"", string.Empty).Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk").Trim());
             }
             catch
             {
                 if (string.IsNullOrEmpty(response.CharacterSet))
                 {
                     encoding = Encoding.UTF8;
                 }
                 else
                 {
                     encoding = Encoding.GetEncoding(response.CharacterSet);
                 }
             }
         }
         else
         {
             if (string.IsNullOrEmpty(response.CharacterSet))
             {
                 encoding = Encoding.UTF8;
             }
             else
             {
                 encoding = Encoding.GetEncoding(response.CharacterSet);
             }
         }
     }
 }
Esempio n. 3
0
        /// <summary>
        /// 获取数据的并解析的方法
        /// </summary>
        /// <param name="item"></param>
        /// <param name="result"></param>
        private void GetData(HttpItem item, HttpResult result)
        {
            #region base
            //获取StatusCode
            result.StatusCode = response.StatusCode;
            //获取StatusDescription
            result.StatusDescription = response.StatusDescription;
            //获取Headers
            result.Header = response.Headers;
            //获取CookieCollection
            if (response.Cookies != null) result.CookieCollection = response.Cookies;
            //获取set-cookie
            if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"];
            #endregion

            #region byte
            //处理网页Byte
            byte[] ResponseByte = GetByte();
            #endregion

            #region Html
            if (ResponseByte != null & ResponseByte.Length > 0)
            {
                //设置编码
                SetEncoding(item, result, ResponseByte);
                //得到返回的HTML
                result.Html = encoding.GetString(ResponseByte);
            }
            else
            {
                //没有返回任何Html代码
                result.Html = string.Empty;
            }
            #endregion
        }
Esempio n. 4
0
 /// <summary>
 /// 鏍规嵁鐩镐紶鍏ョ殑鏁版嵁锛屽緱鍒扮浉搴旈〉闈㈡暟鎹?
 /// </summary>
 /// <param name="item">鍙傛暟绫诲璞?/param>
 /// <returns>杩斿洖HttpResult绫诲瀷</returns>
 public HttpResult GetHtml(HttpItem item)
 {
     //杩斿洖鍙傛暟
     HttpResult result = new HttpResult();
     try
     {
         //鍑嗗鍙傛暟
         SetRequest(item);
     }
     catch (Exception ex)
     {
         result.Cookie = string.Empty;
         result.Header = null;
         result.Html = ex.Message;
         result.StatusDescription = "閰嶇疆鍙傛暟鏃跺嚭閿欙細" + ex.Message;
         //閰嶇疆鍙傛暟鏃跺嚭閿?
         return result;
     }
     try
     {
         //璇锋眰鏁版嵁
         using (response = (HttpWebResponse)request.GetResponse())
         {
             GetData(item, result);
         }
     }
     catch (WebException ex)
     {
         if (ex.Response != null)
         {
             using (response = (HttpWebResponse)ex.Response)
             {
                 GetData(item, result);
             }
         }
         else
         {
             result.Html = ex.Message;
         }
     }
     catch (Exception ex)
     {
         result.Html = ex.Message;
     }
     if (item.IsToLower) result.Html = result.Html.ToLower();
     return result;
 }
Esempio n. 5
0
        /// <summary>
        /// 鑾峰彇鏁版嵁鐨勫苟瑙f瀽鐨勬柟娉?
        /// </summary>
        /// <param name="item"></param>
        /// <param name="result"></param>
        private void GetData(HttpItem item, HttpResult result)
        {
            #region base
            //鑾峰彇StatusCode
            result.StatusCode = response.StatusCode;
            //鑾峰彇StatusDescription
            result.StatusDescription = response.StatusDescription;
            //鑾峰彇Headers
            result.Header = response.Headers;
            //鑾峰彇CookieCollection
            if (response.Cookies != null) result.CookieCollection = response.Cookies;
            //鑾峰彇set-cookie
            if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"];
            #endregion

            #region byte
            //澶勭悊缃戦〉Byte
            byte[] ResponseByte = GetByte();
            #endregion

            #region Html
            if (ResponseByte != null & ResponseByte.Length > 0)
            {
                //璁剧疆缂栫爜
                SetEncoding(item, result, ResponseByte);
                //寰楀埌杩斿洖鐨凥TML
                result.Html = encoding.GetString(ResponseByte);
            }
            else
            {
                //娌℃湁杩斿洖浠讳綍Html浠g爜
                result.Html = string.Empty;
            }
            #endregion
        }
Esempio n. 6
0
        private void TestImportSamples()
        {
            //推荐方式
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem();
            HttpResult hres = new HttpResult();
            item.ContentType = "application/x-www-form-urlencoded";
            item.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
            item.URL = "http://192.168.183.137/api";
            item.Method = "post";
            item.PostDataType = PostDataType.String;
            item.PostEncoding = Encoding.UTF8;
            item.KeepAlive = false;
            item.ProxyIp = "";

            for (int i= 1; i <2; i++)
            {
                for (int k = 1; k < 501; k++)
                {
                    string s2 = "username=XYS&password=123456&method=import_samples&&create_storage=Tem,XYS,7&box_type=bag&json=[{\"Name\":\"1000456613\",\"Sample Type\":\"组织--心研所\",\"Freezer\":\"Tem\",\"Level1\":\"XYS\",\"Level2\":\"7\",\"Box\":\"5\"}]";
                    item.Postdata = s2;
                string  RES =   http.GetHtml(item).Html;
                }
            }
        }