Example #1
0
        public static string HttpJsonPost(string posturl, string postData)
        {
            string       content  = string.Empty;
            Stream       instream = null;
            StreamReader sr       = null;
            Encoding     encoding = Encoding.UTF8;

            byte[] data = encoding.GetBytes(postData);
            try
            {
                // 设置参数
                HttpWebRequest  request         = WebRequest.Create(posturl) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                if (request != null)
                {
                    //Uri u = new Uri("http://test.telefen.com");
                    //Cookie c = new Cookie("UserToken", "VA4vpmdbKOw8HYSR3Z03Vi0wzlSncW2ho2voZLtuwO2o94vZi1Valp29+PgF/0FpqYjsd42Xky+zlfGS06khF2h5giLCPrAAo7x63jmqS5XwbBD2J9LDTnaaCuTxqqCm");

                    request.CookieContainer = cookieContainer;
                    //request.CookieContainer.Add(u, c);
                    request.AllowAutoRedirect = true;//返回301、302再次访问
                    request.Method            = "POST";
                    //request.Referer = "http://esf.bxgfw.com/shop/admin/renthouse_pub.aspx";//伪造从哪个地址访问
                    request.ContentType = "application/x-www-form-urlencoded";
                    //request.ContentType = "application/json; charset=utf-8";//WEBAPI等支持JSON解码
                    //myRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";//模拟浏览器
                    request.ContentLength = data.Length;
                    Stream outstream = request.GetRequestStream();
                    outstream.Write(data, 0, data.Length);
                    outstream.Close();
                    Stopwatch watch = new Stopwatch();//计时器
                    watch.Start();
                    //发送请求并获取相应回应数据
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                    watch.Stop();
                    //直到request.GetResponse()程序才开始向目标网页发送Post请求
                    if (response != null)
                    {
                        instream = response.GetResponseStream();
                    }
                    if (instream != null)
                    {
                        sr = new StreamReader(instream, encoding);
                    }
                    //返回结果网页(html)代码
                    if (sr != null)
                    {
                        content = sr.ReadToEnd();
                        return(content);
                    }
                }
                return(content);
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                UnionLog.WriteLog(LogType.UNION_ERROR, string.Format("Http Post请求异常:{0};堆栈信息:{1}", err, ex.StackTrace));
                return(string.Empty);
            }
        }
Example #2
0
        public static void WriteLog(LogType type, object message)
        {
            switch (type)
            {
            case LogType.UNION_DEBUG:
                log.Debug(message);
                break;

            case LogType.UNION_ERROR:
                UnionLog.WriteLog(LogType.UNION_INFO, message);
                break;

            case LogType.UNION_INFO:
                log.Info(message);
                break;

            case LogType.UNION_WARN:
                log.Warn(message);
                break;

            default:
                break;
            }
        }