コード例 #1
0
        /// <summary>
        /// 以特定的方式url编码
        /// </summary>
        /// <param name="url"></param>
        /// <param name="pageEncode"></param>
        /// <returns></returns>
        public static string UrlEncode(string url,PageEncode pageEncode)
        {
            byte[] bytes = null;
            if (pageEncode==PageEncode.gb2312)
            {
                bytes = Encoding.GetEncoding("gb2312").GetBytes(url);
            }

            if (pageEncode==PageEncode.utf8)
            {
                bytes = Encoding.UTF8.GetBytes(url);
            }

            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                if (bytes[i] < 128)
                {
                    stringBuilder.Append((char)bytes[i]);
                }
                else
                {
                    stringBuilder.Append("%" + bytes[i++].ToString("x").PadLeft(2, '0'));
                    stringBuilder.Append("%" + bytes[i].ToString("x").PadLeft(2, '0'));
                }
            }
            return stringBuilder.ToString();
        }
コード例 #2
0
        /// <summary>
        /// 以post的方式向服务器发送数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="_url"></param>
        /// <returns></returns>
        public static string PostDataToServer(string data, string _url, PageEncode pageEncode)
        {
            string text = "POST";
            string result;
            try
            {
                HttpWebRequest httpWebRequest = WebRequest.Create(_url) as HttpWebRequest;
                string a;
                byte[] bytes = null;
                if ((a = text) != null)
                {
                    if (!(a == "GET"))
                    {
                        if (a == "POST")
                        {
                            httpWebRequest.Method = "POST";

                            switch (pageEncode)
                            {
                                case PageEncode.utf8:
                                    bytes = Encoding.UTF8.GetBytes(data);
                                    httpWebRequest.ContentType = "application/xml;charset=utf-8";
                                    break;
                                case PageEncode.gb2312:
                                    bytes = Encoding.GetEncoding("gb2312").GetBytes(data);
                                    httpWebRequest.ContentType = "application/xml;charset=gb2312";
                                    break;
                                default:
                                    bytes = Encoding.UTF8.GetBytes(data);
                                    httpWebRequest.ContentType = "application/xml;charset=utf-8";
                                    break;
                            }
                            httpWebRequest.ContentLength = (long)bytes.Length;
                            Stream requestStream = httpWebRequest.GetRequestStream();
                            requestStream.Write(bytes, 0, bytes.Length);
                            requestStream.Close();
                        }
                    }
                    else
                    {
                        httpWebRequest.Method = "GET";
                    }
                }
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream);
                string text2 = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
                httpWebResponse.Close();
                result = text2;
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }