码迷,mamicode.com
首页 > Web开发 > 详细

(转载)利用HttpWebRequest通过POST Json数据在.net后台实现不同平台间的数据传输

时间:2014-11-22 11:50:41      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   color   os   sp   数据   on   

/// <summary>
       /// 返回JSon数据
       /// </summary>
       /// <param name="JSONData">要处理的JSON数据</param>
       /// <param name="Url">要提交的URL</param>
       /// <returns>返回的JSON处理字符串</returns>
        public string GetResponseData(string JSONData,string Url)
        {byte[] bytes = Encoding.UTF8.GetBytes(JSONData)
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
        request.ContentLength = bytes.Length;
            request.ContentType = "text/xml";
            Stream reqstream = request.GetRequestStream();
            reqstream.Write(bytes, 0, bytes.Length);

            //声明一个HttpWebRequest请求
            request.Timeout = 90000;
            //设置连接超时时间
            request.Headers.Set("Pragma", "no-cache");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamReceive = response.GetResponseStream();
            Encoding encoding = Encoding.UTF8;

            StreamReader streamReader = new StreamReader(streamReceive, encoding);
            string  strResult = streamReader.ReadToEnd();
            streamReceive.Dispose();
            streamReader.Dispose();

            return strResult;
        }

如果不通过request.write()的方式向接口post数据,将request.ContentLength属性设置为0,让后去掉request.write()方法即可!

来源:http://blog.csdn.net/uniqer/article/details/14170297

(转载)利用HttpWebRequest通过POST Json数据在.net后台实现不同平台间的数据传输

标签:style   blog   http   ar   color   os   sp   数据   on   

原文地址:http://www.cnblogs.com/blog4xy/p/4114936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!