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;
}
c#访问webapi以及获取的更多相关文章
- 利用HttpWebRequest访问WebApi
WebApi现在越来越流行,下面给出利用HttpWebRequest访问WebApi的工具方法: 1.利用基准URL和参数字典生成完整URL /// <summary> /// 生成URL ...
- 使用react全家桶制作博客后台管理系统 网站PWA升级 移动端常见问题处理 循序渐进学.Net Core Web Api开发系列【4】:前端访问WebApi [Abp 源码分析]四、模块配置 [Abp 源码分析]三、依赖注入
使用react全家桶制作博客后台管理系统 前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用react全家桶制作的博客后台管理系统 概述 该项目是基 ...
- 转 Postman访问Webapi的Get/Post/Put/Delte请求
Postman访问Webapi的Get/Post/Put/Delte请求 2018年07月26日 15:04:46 DoNotWorkOvertime 阅读数:348 标签: WebApiPostma ...
- Android 访问 Webapi 更新UI
首先,写一个访问webapi的工具类 import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import or ...
- 浏览器跨域访问WebApi
webapi地址:wapapi.ebcbuy.com web地址:wapweb.ebcbuy.com 在默认情况下这两个域名属于两个不同的域,他们之间的交互存在跨域的问题,但因为他们都同属于一 ...
- html网页访问WebAPI中的方法遇到的问题
1.移动端访问远程服务时,建议使用WebAPI 2.用不同浏览器访问WebAPI时返回的文本格式是不同的,Chrome Firefox将在浏览器中以XML形式显示此列表,IE浏览器将获得Json格 ...
- C# 通过HTTP代理访问Socket来获取邮件
C# 通过HTTP代理访问Socket来获取邮件 关键穿透代理的代码(通过HTTP代理获取TcpClent) public class ClientHelper { public static Tcp ...
- layui表单提交与ajax访问webapi
啊啊啊啊 这个东西实在很蛋疼啊 每次访问webapi就很老火 这里就一下 以后忘记的话就来查阅 不多说 直接开始 首先html页面 新建一个基于layui的form表单页面LayuiForm.csh ...
- 十二、.net core(.NET 6)添加通用的访问webapi的方法(包括HttpClient和HttpWebRequest)
开发通用的访问webapi方法. 在common工具文件夹下,新建一个类库项目:Wsk.Core.WebHelper,并引用Package包项目,然后新建一个类HttpClientHelper,用于使 ...
随机推荐
- WinForm:如何在ListBox中添加CheckBox
http://www.cnblogs.com/myshell/archive/2010/09/24/1834184.html 最近因为做WinForm的项目,遇到这个问题,当时以为CheckedLis ...
- 案例 element 表单名两端对齐
>>> .el-form-item label:after { content: ""; display: inline-block; width: 100%; ...
- sqlserver sp_who2和inputbuffer的使用,连接数
一.sp_who2的使用 1.存储过程的位置 sp_who官方解释地址:https://docs.microsoft.com/zh-cn/sql/relational-databases/system ...
- Ansible自动化运维工具(1)
1. Ansible的架构 Ansible的帮助文档: http://www.ansible.com.cn/index.html 2. YAML语言简介 基本规则 列表(list, [, , , .. ...
- OGG-01161
Bad column index (35) specified for table user.table_name, max columns = 35. 原因:源端表结构发生了变更 解决办法:1.如果 ...
- join优化
1.left outer join先执行连接操作,再将结果通过WHERE语句进行过滤 select s.ymd,s.symbol,s.price_close,d.dividend from stock ...
- 【容器化】容器技术实践.pdf_视频学习笔记
容器运行时 docker rkt gvisor containerd 容器编排系统:kubernetes (简称k8s)
- bzoj5178 [Jsoi2011]棒棒糖 主席树+线段树二分
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5178 https://lydsy.com/JudgeOnline/problem.php?id ...
- css 表单头部固定
原创 https://blog.csdn.net/q3585914/article/details/69946478 table表头和首列的表格固定-CSS实现的Table表头固定 原创 2017年0 ...
- struts2+ajax 前后端传值
摘要: 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的数据,并转换为json类型模式数据 3.配置struts.xml文件 4.页面脚本接受并处理数 ...