博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 用POST提交json数据
阅读量:6991 次
发布时间:2019-06-27

本文共 1195 字,大约阅读时间需要 3 分钟。

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();
}

转载于:https://www.cnblogs.com/xirilingfeng/p/3816394.html

你可能感兴趣的文章
11G、12C Data Guard Physical Standby Switchover转换参考手册
查看>>
root.sh脚本支持checkpoints文件实现重复运行
查看>>
Algs4-2.4.20证明:基于下沉的堆构造方法的比较次数、交换次数
查看>>
16进制的简单运算http://acm.nyist.net/JudgeOnline/problem.php?pid=244
查看>>
leetcode3. Longest Substring Without Repeating Characters
查看>>
Jmeter之Bean shell使用
查看>>
C#中泛型的使用笔记
查看>>
【bzoj4009 hnoi2015】接水果
查看>>
@property专题
查看>>
LNMP结合discuz的配置
查看>>
js中ul与li的使用
查看>>
实验二
查看>>
jquery.artDialog.source.js学习
查看>>
PDF去除签名
查看>>
socket
查看>>
date
查看>>
需求方如何选择优秀的项目外包团队?
查看>>
python笔记目录
查看>>
Java语法基础课 原码 反码 补码
查看>>
Nginx记录客户端POST过来的具体信息
查看>>