コード例 #1
0
        public string HttpPostData(string url, object paramter, HttpItem pitem = null)
        {
            try
            {
                if (paramter == null)
                {
                    CommunicationHelper.TraceException("HttppostData", "null parameter", "parameter obj is null");
                }

                string contentType = "application/json";

                if (paramter is string || paramter.GetType().IsValueType)
                {
                    contentType = "application/x-www-form-urlencoded";
                }

                var postStr = CommunicationHelper.SerializeObjToJsonStr(paramter);
                if (pitem == null)
                {
                    pitem = new HttpItem()
                    {
                        Method      = "Post",
                        Postdata    = postStr,
                        ContentType = "application/x-www-form-urlencoded",
                        Encoding    = Encoding.UTF8,
                        URL         = url
                    };
                }
                else
                {
                    pitem.Method   = "Post";
                    pitem.Postdata = postStr;
                    //pitem.ContentType = contentType;
                    pitem.URL = url;
                }

                var result = _engine.GetHtml(pitem);
                CommunicationHelper.RecordTrace("HttpPostData", "返回结果:" + result.Html);
                return(result.Html);

                //var client = new KRWebClient { Timeout = Timeout };
                //NameValueCollection param = new NameValueCollection();
                ////client.Headers.Add("Content-Type", "application/json");
                //param.Add("type", type);
                //param.Add("content", jsonStr);
                //var content = client.UploadValues(url, param);
                //var strContent = Encoding.UTF8.GetString(content);
                //return strContent;
                //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                //request.Method = "Post";
                //request.ContentType = "application/json";
                //request.ContentLength = _encoding.GetByteCount(jsonStr);
                //Stream myRequestStream = request.GetRequestStream();
                //StreamWriter myStreamWriter = new StreamWriter(myRequestStream);
                //myStreamWriter.Write(jsonStr);
                //myStreamWriter.Close();

                //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                //Stream myResponseStream = response.GetResponseStream();
                //StreamReader myStreamReader = new StreamReader(myResponseStream);
                //retString = myStreamReader.ReadToEnd();
                //myStreamReader.Close();
                //myResponseStream.Close();
            }
            catch (Exception ex)
            {
                ClientHelper.TraceException("HttpPostData:Post", "Post数据失败", ex.Message);
                return(string.Empty);
            }

            //return HttpUtility.UrlDecode(retString);
        }