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

C# 用POST提交json数据

时间:2014-07-01 23:52:35      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:style   http   数据   os   io   type   

public void GetResponse(string url, string json)
{

Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(json);
//此处为为http请求url
var uri = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
//用此方法可以添加标准或非标准http请求,诸如conten-type ,accept,range等
request.Headers.Add("X-Auth-Token", System.Web.HttpUtility.UrlEncode("openstack"));
//此处为C#实现的一些标准http请求头添加方法,用上面的方面也可以实现
request.ContentType = "application/json";
request.Accept = "application/json";
// request.ContentLength = data.Length;
//此处添加标准http请求方面
request.Method = "POST";
System.IO.Stream sm = request.GetRequestStream();
sm.Write(data, 0, data.Length);
sm.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse, Encoding.UTF8);
Char[] readBuff = new Char[256];
int count = streamRead.Read(readBuff, 0, 256);
//content为http响应所返回的字符流
String content = "";
while (count > 0)
{
String outputData = new String(readBuff, 0, count);
content += outputData;
count = streamRead.Read(readBuff, 0, 256);
}
response.Close();
}

C# 用POST提交json数据,布布扣,bubuko.com

C# 用POST提交json数据

标签:style   http   数据   os   io   type   

原文地址:http://www.cnblogs.com/xirilingfeng/p/3816394.html

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