C# HTTP系列3 HttpWebRequest.ContentType属性

Stella981
• 阅读 318

系列目录     【已更新最新开发文章,点击查看详细】

获取或设置请求的 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-typeHTTP标头时替换任何现有内容。

要清除Content-typeHTTP标头,请将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 }

系列目录     【已更新最新开发文章,点击查看详细】

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Souleigh ✨ Souleigh ✨
3年前
前端性能优化 - 雅虎军规
无论是在工作中,还是在面试中,web前端性能的优化都是很重要的,那么我们进行优化需要从哪些方面入手呢?可以遵循雅虎的前端优化35条军规,这样对于优化有一个比较清晰的方向.35条军规1.尽量减少HTTP请求个数——须权衡2.使用CDN(内容分发网络)3.为文件头指定Expires或CacheControl,使内容具有缓存性。4.避免空的
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
2年前
1. 容器化部署一套云服务 第一讲 Jenkins(Docker + Jenkins + Yii2 + 云服务器))
容器化部署一套云服务系列1\.容器化部署一套云服务之Jenkins(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.cnblogs.com%2Fjackson0714%2Fp%2Fdeploy1.html)一、购买服务器服务器!caeef00
Stella981 Stella981
2年前
Docker 部署SpringBoot项目不香吗?
  公众号改版后文章乱序推荐,希望你可以点击上方“Java进阶架构师”,点击右上角,将我们设为★“星标”!这样才不会错过每日进阶架构文章呀。  !(http://dingyue.ws.126.net/2020/0920/b00fbfc7j00qgy5xy002kd200qo00hsg00it00cj.jpg)  2
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
C#4.0新增功能02 命名实参和可选实参
系列目录    【已更新最新开发文章,点击查看详细】(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.cnblogs.com%2FSavionZhang%2Fp%2F11229640.html)C4介绍命名实参和可选实参。 通过_命名实参_,你可以为特