c# 键值对的方式post提交
DataContractJsonSerializer jsQcData = new DataContractJsonSerializer(typeof(DATA<data>));//DATA<data>要转换成json的值
MemoryStream msObj = new MemoryStream();
jsQcData.WriteObject(msObj, devQcData);
msObj.Position = 0;
//从0这个位置开始读取流中的数据
StreamReader Stream = new StreamReader(msObj, Encoding.UTF8);
string strQcJson = Stream.ReadToEnd();
qcStream.Close();
msObj.Close();
Dictionary<string, string> dic = new Dictionary<string, string>();//以键值对方式post提交
dic .Add("keyname", value);//value=strQcJson ;
string str = Post(path, dic );
/// <summary>
/// 指定Post地址使用Get 方式获取全部字符串
/// </summary>
/// <param name="url">请求后台地址</param>
/// <returns></returns>
public static string Post(string url, Dictionary<string, string> dic)
{
string result = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
#region 添加Post 参数
StringBuilder builder = new StringBuilder();
int i = 0;
foreach (var item in dic)
{
if (i > 0)
builder.Append("&");
builder.AppendFormat("{0}={1}", item.Key, item.Value);
i++;
}
byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
#endregion
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
return result;
}