HttpWebrequest的使用

public class HttpPost
    {
        public string SendAll(string Url, string jsonString)
        {
            #region 发送HTTP POST 数据

            HttpWebRequest httpWebRequest = WebRequest.Create(Url) as HttpWebRequest;//定义对象

            httpWebRequest.Method = "POST";//设置提交方式 支持POST PUT GET DELETE
            httpWebRequest.ContentType = "application/json"; //设置内容类型
            httpWebRequest.Timeout = 60000;//设置超时时间

            byte[] data = System.Text.Encoding.UTF8.GetBytes(jsonString);//将发送字符转化为 Byte[]

            using (Stream stream = httpWebRequest.GetRequestStream())//用stream进行发送
            {
                stream.Write(data, 0, data.Length);//写入
            }

            Stream responseStream = httpWebRequest.GetResponse().GetResponseStream();//获取响应流

            string result = string.Empty;
            using (StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8))
            {
                result = responseReader.ReadToEnd();//读取响应流
            }
            return result;//返回结果
            #endregion
        }
    }

 

posted @ 2013-05-06 13:51  瑟秋先生  阅读(268)  评论(0编辑  收藏  举报