首页 > 开发 > .net > 正文

c#实现post

2015-08-30 22:40:21  来源: 网友分享
public string Post(string postDataStr)
        {
            request.Method = "post";
            request.ContentType = "text/xml";
            request.Headers.Add("charset:utf-8");
            var encoding = Encoding.GetEncoding("utf-8");
            byte[] buffer = encoding.GetBytes(postDataStr);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            using (HttpWebResponse wr = request.GetResponse() as HttpWebResponse)
            {
                using (StreamReader reader = new StreamReader(wr.GetResponseStream(), encoding))
                {
                    return reader.ReadToEnd();
                }
            }
        }