C# 用POST提交json数据

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数据

上一篇:php-fpm启动


下一篇:百度 flash html5自切换 多文件异步上传控件webuploader基本用法