c#访问webapi以及获取
提交post
#region XML方式提交
public static void XML() {
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://localhost:30447/api/product/showname");
wReq.Method = "POST";
wReq.ContentType = "text/xml";
wReq.Headers.Add("charset:utf-8");
var encoding = Encoding.GetEncoding("utf-8");
if (GetXml() != null)
{
byte[] buffer = encoding.GetBytes(GetXml());
wReq.ContentLength = buffer.Length;
wReq.GetRequestStream().Write(buffer, 0, buffer.Length);
}
else {
wReq.ContentLength = 0;
}
}
/// <summary>
/// 发送的XML
/// </summary>
/// <returns></returns>
public static string GetXml() {
StringBuilder str = new StringBuilder();
str.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
str.Append("<Product>");
str.Append("<Id>456</Id>");
str.Append("<Name>ASDD</Name>");
str.Append("<Categroy>QWER</Categroy>");
str.Append("<Price>456</Price>");
str.Append("</Product>");
return str.ToString();
}
#endregion
#region Text提交方法
public static void TEXT() {
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://localhost:30447/api/product/showname");
wReq.Method = "POST";
wReq.ContentType = "text/plain";
byte[] data = Encoding.Default.GetBytes("Id:798,Name:\"QW\",Categroy:\"ajsdkf\",Price:789");
wReq.ContentLength = data.Length;
Stream reqStream = wReq.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
using (StreamReader sr = new StreamReader(wReq.GetResponse().GetResponseStream()))
{
string result = sr.ReadToEnd();
}
}
#endregion
#region JSON发送方法
/// <summary>
/// JSON发送方法
/// </summary>
public static void Json() {
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://localhost:30447/api/product/showname");
wReq.Method = "POST";
wReq.ContentType = "application/JSON";
byte[] data = Encoding.Default.GetBytes("{Id:123,Name:\"zwy\",Categroy:\"ajsdkf\",Price:123}");
wReq.ContentLength = data.Length;
Stream reqStream = wReq.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
using (StreamReader sr = new StreamReader(wReq.GetResponse().GetResponseStream()))
{
string result = sr.ReadToEnd();
}
}
#endregion
#region Form提交
public static void Froms()
{
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://localhost:30447/api/product/showname");
wReq.Method = "POST";
wReq.ContentType = "application/x-www-form-urlencoded";
string str = "Id:123,Name:\"zwy\",Categroy:\"ajsdkf\",Price:123";
byte[] data = Encoding.Default.GetBytes(str);
wReq.ContentLength = data.Length;
Stream reqStream = wReq.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
using (StreamReader sr = new StreamReader(wReq.GetResponse().GetResponseStream()))
{
string result = sr.ReadToEnd();
}
}
#endregion
获取
[HttpPost]
public Product ShowName()
{
var prod=new Product();
var s = System.Web.HttpContext.Current.Request.InputStream;
var b = new byte[s.Length];
s.Read(b, 0, (int)s.Length);
var str = Encoding.UTF8.GetString(b);
try
{
//如果不是JSON报错
var serializer = new JavaScriptSerializer();
dynamic obj = serializer.Deserialize(str, typeof(object));
//prod = serializer.Deserialize<Product>(str);
}
catch (Exception ex)
{
try
{
//如果不是xml,也不是json
var d = new XmlDocument();
d.LoadXml(str);
//prod= DeserializeToObject<Product>(str);
}
catch (Exception e)
{
//text文本
string index = str;
}
}
return prod;
}
最新文章
- SQL-日期函数
- Node学习笔记(二):事件驱动
- PHP curl 函数
- git --- push到远端
- case when then else end
- python matplotlib画图产生的Type 3 fonts字体没有嵌入问题
- 二、CSS 基本介绍
- WCF入门教程(三)属性标签
- SQL Server - 聚集索引 <;第六篇>;
- 宝更容易使用比读IC卡信息的工具
- Eclipse中Lombok的安装和注解说明
- JUnit测试提示Java.lang.Exception: No runnable methods
- [宏]preempt_disable
- Android开发中遇到的问题(二)——新建android工程的时候eclipse没有生成MainActivity和layout布局
- 转:Linux实时将所有输出重定向到文件
- 惭愧, eclipse 之 build path
- InnoDB Master Thread I/O Rate详解
- [转载]从B 树、B+ 树、B* 树谈到R 树
- Sub-process /usr/bin/dpkg returned an error code (1)解决方法
- .net图表之ECharts随笔08-bar柱状图