编程语言
首页 > 编程语言> > C# Post请求数据

C# Post请求数据

作者:互联网

        /// <summary>
        /// 获取access_token
        /// </summary>
        /// <returns></returns>
        public string GetToken()
        {
            string url = "https://XXX.com/oauth/token?client_id=XX";
byte[] data = System.Text.Encoding.UTF8.GetBytes("");//参数
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);

  myRequest.Headers["Authorization"] = "XX"; //添加头 myRequest.Method = "POST";//请求类型 Get,Post.... myRequest.ContentType = "application/xml;charset=UTF-8";//内容格式


myRequest.ContentLength = data.Length; Stream newStream = myRequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); HttpWebResponse myResponse = myRequest.GetResponse() as HttpWebResponse; StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); string content = reader.ReadToEnd(); reader.Close(); return content; }

 

标签:请求,C#,content,newStream,myRequest,reader,Post,data,string
来源: https://www.cnblogs.com/zhuzhi0819/p/13219711.html