Ejemplo n.º 1
0
        public static string HttpPost(string Url, Dictionary <string, object> param)
        {
            Dictionary <string, object> endParam = new Dictionary <string, object>();

            Dictionary <string, object> paramHead = new Dictionary <string, object>();

            paramHead.Add("osType", "1");
            paramHead.Add("accessToken", MainForm.accessToken);
            paramHead.Add("type", "1");
            paramHead.Add("userId", MainForm.userId);
            paramHead.Add("version", "1.0");

            endParam.Add("body", param);
            endParam.Add("head", paramHead);

            string postDataStr = JsonConvert.SerializeObject(endParam);


            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method      = "POST";
                request.ContentType = "application/json;charset=utf-8";
                request.ServicePoint.Expect100Continue = false;
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;//SecurityProtocolType.Tls1.2;
                //request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
                Stream myRequestStream = request.GetRequestStream();
                //StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("utf-8"));
                myStreamWriter.Write(postDataStr);
                myStreamWriter.Close();
                HttpWebResponse response         = (HttpWebResponse)request.GetResponse();
                Stream          myResponseStream = response.GetResponseStream();
                StreamReader    myStreamReader   = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string          retString        = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                JObject     jsonObj     = JObject.Parse(retString);
                string      head        = jsonObj["head"].ToString();
                HeaderModel headerModel = new HeaderModel();
                headerModel = (HeaderModel)JsonToObject(head, headerModel);

                if (Convert.ToInt16(headerModel.rspCode) == -9)
                {
                    MainForm.accessToken = null;
                    return("accessToken");
                }
                else
                {
                    return(retString);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return("");
            }
        }