获取或设置请求的 Content-type
HTTP 标头的值。默认值为null
。
常见的请求内容类型为以下几种:
1 /// <summary> 2 /// HTTP 内容类型(Content-Type) 3 /// </summary> 4 public class HttpContentType 5 { 6 /// <summary> 7 /// 资源类型:普通文本 8 /// </summary> 9 public const string TEXT_PLAIN = "text/plain"; 10 11 /// <summary> 12 /// 资源类型:JSON字符串 13 /// </summary> 14 public const string APPLICATION_JSON = "application/json"; 15 16 /// <summary> 17 /// 资源类型:未知类型(数据流) 18 /// </summary> 19 public const string APPLICATION_OCTET_STREAM = "application/octet-stream"; 20 21 /// <summary> 22 /// 资源类型:表单数据(键值对) 23 /// </summary> 24 public const string WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; 25 26 /// <summary> 27 /// 资源类型:表单数据(键值对)。编码方式为 gb2312 28 /// </summary> 29 public const string WWW_FORM_URLENCODED_GB2312 = "application/x-www-form-urlencoded;charset=gb2312"; 30 31 /// <summary> 32 /// 资源类型:表单数据(键值对)。编码方式为 utf-8 33 /// </summary> 34 public const string WWW_FORM_URLENCODED_UTF8 = "application/x-www-form-urlencoded;charset=utf-8"; 35 36 /// <summary> 37 /// 资源类型:多分部数据 38 /// </summary> 39 public const string MULTIPART_FORM_DATA = "multipart/form-data"; 40 }
提交的时候可以说明编码的方式,用来使对方服务器能够正确的解析。
该ContentType的属性包含请求的媒体类型。分配给ContentType属性的值在请求发送Content-type
HTTP标头时替换任何现有内容。
要清除Content-type
HTTP标头,请将ContentType属性设置为null
。
此属性的值存储在WebHeaderCollection中。如果设置了WebHeaderCollection,则属性值将丢失。
参考示例代码:
1 private HttpResult Request(string url, string data, string method, string contentType) 2 { 3 HttpResult httpResult = new HttpResult(); 4 HttpWebRequest httpWebRequest = null; 5 6 try 7 { 8 httpWebRequest = WebRequest.Create(url) as HttpWebRequest; 9 httpWebRequest.Method = method; 10 httpWebRequest.Headers = HeaderCollection; 11 httpWebRequest.CookieContainer = CookieContainer;
12 /*此属性的值存储在WebHeaderCollection中。如果设置了WebHeaderCollection,则属性值将丢失。 13 *所以放置在Headers 属性之后设置 14 */ 15 httpWebRequest.ContentType = contentType; 16 httpWebRequest.UserAgent = _userAgent; 17 httpWebRequest.AllowAutoRedirect = _allowAutoRedirect; 18 httpWebRequest.ServicePoint.Expect100Continue = false; 19 20 if (data != null) 21 { 22 httpWebRequest.AllowWriteStreamBuffering = true; 23 using (Stream requestStream = httpWebRequest.GetRequestStream()) 24 { 25 requestStream.Write(EncodingType.GetBytes(data), 0, data.Length); 26 requestStream.Flush(); 27 } 28 } 29 30 HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; 31 if (httpWebResponse != null) 32 { 33 GetResponse(ref httpResult, httpWebResponse); 34 httpWebResponse.Close(); 35 } 36 } 37 catch (WebException webException) 38 { 39 GetWebExceptionResponse(ref httpResult, webException); 40 } 41 catch (Exception ex) 42 { 43 GetExceptionResponse(ref httpResult, ex, method, contentType); 44 } 45 finally 46 { 47 if (httpWebRequest != null) 48 { 49 httpWebRequest.Abort(); 50 } 51 } 52 53 return httpResult; 54 }
个人作品
1、BIMFace.Community.SDK.NET
开源地址:https://gitee.com/NAlps/BIMFace.SDK`
2、ZCN.NET.Common
开源地址:https://gitee.com/NAlps/zcn.net.common
技术栈
1、Visual Studio、.C#/.NET、.NET Core、MVC、Web API、RESTful API、gRPC、SignalR、Python
2、jQuery、Vue.js、Bootstrap
3、数据库:SQLServer、MySQL、PostgreSQL、Oracle、SQLite、Redis、MongoDB、ElasticSearch、TiDB、达梦DM、人大金仓、 神通、南大通用 GBase、华为 GaussDB 、腾讯 TDSQL 、阿里 PolarDB、蚂蚁金服 OceanBase、东软 OpenBASE、浪潮云溪数据库 ZNBase
4、ORM:Dapper、Entity Framework、FreeSql、SqlSugar、分库分表、读写分离
5、架构:领域驱动设计 DDD、ABP
6、环境:跨平台、Windows、Linux(CentOS、麒麟、统信UOS、深度Linux)、maxOS、IIS、Nginx、Apach
7、移动App:Android、IOS、HarmonyOS、微信、小程序、快应用、Xamarin、uni-app、MUI、Flutter、Framework7、Cordova、Ionic、React Native、Taro、NutUI、Smobiler
云原生、微服务、Docker、CI/CD、DevOps、K8S;
Dapr、RabbitMQ、Kafka、分布式、大数据、高并发、负载均衡、中间件、RPC、ELK;
.NET + Docker + jenkins + Github + Harbor + K8S;·
·
作者:张传宁 微软MCP、系统架构设计师、系统集成项目管理工程师、科技部创新工程师。
专注于微软.NET技术(.NET Core、Web、MVC、WinForm、WPF)、通用权限管理系统、工作流引擎、自动化项目(代码)生成器、SOA 、DDD、 云原生(Docker、微服务、DevOps、CI/CD);PDF、CAD、BIM 审图等研究与应用。
多次参与电子政务、图书教育、生产制造等企业级大型项目研发与管理工作。
熟悉中小企业软件开发过程:需求分析、架构设计、编码测试、实施部署、项目管理。通过技术与管理帮助中小企业快速化实现互联网技术全流程解决方案。
本文版权归作者·有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。`