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


    1. /// <summary>  
    2.        /// 返回JSon数据  
    3.        /// </summary>  
    4.        /// <param name="JSONData">要处理的JSON数据</param>  
    5.        /// <param name="Url">要提交的URL</param>  
    6.        /// <returns>返回的JSON处理字符串</returns>  
    7.         public string GetResponseData(string JSONData,string Url)  
    8.         {byte[] bytes = Encoding.UTF8.GetBytes(JSONData)  
    9.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);  
    10.             request.Method = "POST";  
    11.         request.ContentLength = bytes.Length;  
    12.             request.ContentType = "text/xml";  
    13.             Stream reqstream = request.GetRequestStream();  
    14.             reqstream.Write(bytes, 0, bytes.Length);  
    15.   
    16.             //声明一个HttpWebRequest请求  
    17.             request.Timeout = 90000;  
    18.             //设置连接超时时间  
    19.             request.Headers.Set("Pragma", "no-cache");  
    20.             HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
    21.             Stream streamReceive = response.GetResponseStream();  
    22.             Encoding encoding = Encoding.UTF8;  
    23.   
    24.             StreamReader streamReader = new StreamReader(streamReceive, encoding);  
    25.             string  strResult = streamReader.ReadToEnd();  
    26.             streamReceive.Dispose();  
    27.             streamReader.Dispose();  
    28.   
    29.             return strResult;  
    30.         }  

    //string serviceAddress = "https://www.culturesclub.com/CultureHomes/api/CSMemberValidation";
    //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);

    //request.Method = "POST";
    //request.ContentType = "application/json";
    //string strContent = @"{ ""client_token"": ""89e"",""member"": ""0101943""}";
    //using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
    //{
    // dataStream.Write(strContent);
    // dataStream.Close();
    //}
    //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    //string encoding = response.ContentEncoding;
    //if (encoding == null || encoding.Length < 1)
    //{
    // encoding = "UTF-8"; //默认编码
    //}
    //StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
    //string retString = reader.ReadToEnd();
    ////解析josn
    //JObject jo = JObject.Parse(retString);
    //textBox1.Text = jo.ToString();

  • 相关阅读:
    接水果(fruit)——整体二分+扫描线
    大融合——LCT维护子树信息
    魔卡少女(cardcaptor)——线段树
    而立之年的一些人生感悟
    PHP 输出缓冲控制(Output Control) 学习
    我所了解的cgi
    c语言指针学习
    ubuntu 安装 zend studio
    Zend_Controller_Front 研究
    php autoload 笔记
  • 原文地址:https://www.cnblogs.com/gq0324/p/8583321.html
Copyright © 2020-2023  润新知