提交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;
        }

c#访问webapi以及获取的更多相关文章

  1. 利用HttpWebRequest访问WebApi

    WebApi现在越来越流行,下面给出利用HttpWebRequest访问WebApi的工具方法: 1.利用基准URL和参数字典生成完整URL /// <summary> /// 生成URL ...

  2. 使用react全家桶制作博客后台管理系统 网站PWA升级 移动端常见问题处理 循序渐进学.Net Core Web Api开发系列【4】:前端访问WebApi [Abp 源码分析]四、模块配置 [Abp 源码分析]三、依赖注入

    使用react全家桶制作博客后台管理系统   前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用react全家桶制作的博客后台管理系统 概述 该项目是基 ...

  3. 转 Postman访问Webapi的Get/Post/Put/Delte请求

    Postman访问Webapi的Get/Post/Put/Delte请求 2018年07月26日 15:04:46 DoNotWorkOvertime 阅读数:348 标签: WebApiPostma ...

  4. Android 访问 Webapi 更新UI

    首先,写一个访问webapi的工具类 import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import or ...

  5. 浏览器跨域访问WebApi

      webapi地址:wapapi.ebcbuy.com web地址:wapweb.ebcbuy.com   在默认情况下这两个域名属于两个不同的域,他们之间的交互存在跨域的问题,但因为他们都同属于一 ...

  6. html网页访问WebAPI中的方法遇到的问题

      1.移动端访问远程服务时,建议使用WebAPI 2.用不同浏览器访问WebAPI时返回的文本格式是不同的,Chrome Firefox将在浏览器中以XML形式显示此列表,IE浏览器将获得Json格 ...

  7. C# 通过HTTP代理访问Socket来获取邮件

    C# 通过HTTP代理访问Socket来获取邮件 关键穿透代理的代码(通过HTTP代理获取TcpClent) public class ClientHelper { public static Tcp ...

  8. layui表单提交与ajax访问webapi

    啊啊啊啊 这个东西实在很蛋疼啊 每次访问webapi就很老火 这里就一下  以后忘记的话就来查阅 不多说 直接开始 首先html页面 新建一个基于layui的form表单页面LayuiForm.csh ...

  9. 十二、.net core(.NET 6)添加通用的访问webapi的方法(包括HttpClient和HttpWebRequest)

    开发通用的访问webapi方法. 在common工具文件夹下,新建一个类库项目:Wsk.Core.WebHelper,并引用Package包项目,然后新建一个类HttpClientHelper,用于使 ...

随机推荐

  1. HDU 2783 You’ll be Working on the Railroad(最短路)

    You’ll be Working on the Railroad Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  2. Ubuntu中找不到pip3命令的解决方法

    Ubuntu中找不到pip3命令的解决方法 Ubuntu 有 python2 和 python3. 今天使用 Ubuntu 中的 python3 时,想要安装第三方库却发现 pip 指向的是 pyth ...

  3. WinForm的RadioButton使用小技巧

    http://www.cnblogs.com/sjrhero/articles/1883155.html 当多个RadioButton同在一个容器里面的时候,多半的操作都是要得到其中一个的值这个时候我 ...

  4. 如何设置 ComboBox 下拉列表的高度或间距

    ComboBox 的下拉列表部分总是很挤,看起不舒服,但是设置了 ItemHeight 没用,怎么办呢? 首先设置一个较大的 ItemHeight 值,比如 20: 然后设置 ComboBox 的 D ...

  5. 本地局域网MySQL数据库连接方法(Navcat权限设置)

    情景: 当同一局域网环境中,需要协同开发并涉及到本地数据库互联情况,有关权限需要开放! 设置流程: 1.受访端连接 打开Navcat>>>>点击本地数据库>>> ...

  6. css文档之盒模型阅读笔记

    前段时间抽空仔细阅读了w3c的css文档关于盒模型方面的一些基础知识.边读边记录了一些要点,在此做些整理,与大家分享,如有理解有误之处,请不吝指教. 1.综述 文档中的每个元素被描绘为矩形盒子.渲染引 ...

  7. 【vue】父子组件间通信----传函数

    (一)子组件 调用 父组件 方法 方式一) 子组件中通过this.$parent.event来调用父组件的方法 父组件 <template> <div> <child&g ...

  8. ffmpeg知多少~~~

    一.ffmpeg安装: https://jingyan.baidu.com/article/f7ff0bfcd64cea2e26bb1334.html   二.ffmpeg视频处理(包括各种视频流处理 ...

  9. case_when

    select sname,score,case when score>=20 and score<30 then 20when score>=30 and score<40 t ...

  10. nginx 重写

    rewrite指令可在 server 块或者 location 块中配置. 语法: rewrite regex replacement [flag]; 1.rewrite 接收的 uri 不包含 ho ...