示例#1
0
        public static string HttpPost(string Url, string postDataStr)
        {
            var retString = string.Empty;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method        = "POST";
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
                Stream       myRequestStream = request.GetRequestStream();
                StreamWriter myStreamWriter  = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                myStreamWriter.Write(postDataStr);
                myStreamWriter.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                Stream       myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader   = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
            }
            catch (Exception ex)
            {
                NLogHelper.WriteError(ex.Message);
            }
            return(retString);
        }
示例#2
0
        public static string HttpGet(string Url, string queryUrl)
        {
            var retString = string.Empty;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (queryUrl == "" ? "" : "?") + queryUrl);
                request.Method      = "GET";
                request.ContentType = "text/html;charset=UTF-8";

                HttpWebResponse response         = (HttpWebResponse)request.GetResponse();
                Stream          myResponseStream = response.GetResponseStream();
                StreamReader    myStreamReader   = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
            }
            catch (Exception ex)
            {
                NLogHelper.WriteError(ex.Message);
            }
            return(retString);
        }