获取或设置请求的 Content-type HTTP 标头的值。默认值为null

常见的请求内容类型为以下几种:

 /// <summary>
/// HTTP 内容类型(Content-Type)
/// </summary>
public class HttpContentType
{
/// <summary>
/// 资源类型:普通文本
/// </summary>
public const string TEXT_PLAIN = "text/plain"; /// <summary>
/// 资源类型:JSON字符串
/// </summary>
public const string APPLICATION_JSON = "application/json"; /// <summary>
/// 资源类型:未知类型(数据流)
/// </summary>
public const string APPLICATION_OCTET_STREAM = "application/octet-stream"; /// <summary>
/// 资源类型:表单数据(键值对)
/// </summary>
public const string WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; /// <summary>
/// 资源类型:表单数据(键值对)。编码方式为 gb2312
/// </summary>
public const string WWW_FORM_URLENCODED_GB2312 = "application/x-www-form-urlencoded;charset=gb2312"; /// <summary>
/// 资源类型:表单数据(键值对)。编码方式为 utf-8
/// </summary>
public const string WWW_FORM_URLENCODED_UTF8 = "application/x-www-form-urlencoded;charset=utf-8"; /// <summary>
/// 资源类型:多分部数据
/// </summary>
public const string MULTIPART_FORM_DATA = "multipart/form-data";
}

提交的时候可以说明编码的方式,用来使对方服务器能够正确的解析。

ContentType的属性包含请求的媒体类型。分配给ContentType属性的值在请求发送Content-typeHTTP标头时替换任何现有内容。

要清除Content-typeHTTP标头,请将ContentType属性设置为null

此属性的值存储在WebHeaderCollection中。如果设置了WebHeaderCollection,则属性值将丢失。

参考示例代码:

 private HttpResult Request(string url, string data, string method, string contentType)
{
HttpResult httpResult = new HttpResult();
HttpWebRequest httpWebRequest = null; try
{
httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
httpWebRequest.Method = method;
httpWebRequest.Headers = HeaderCollection;
httpWebRequest.CookieContainer = CookieContainer;
/*此属性的值存储在WebHeaderCollection中。如果设置了WebHeaderCollection,则属性值将丢失。
*所以放置在Headers 属性之后设置
*/
httpWebRequest.ContentType = contentType;
httpWebRequest.UserAgent = _userAgent;
httpWebRequest.AllowAutoRedirect = _allowAutoRedirect;
httpWebRequest.ServicePoint.Expect100Continue = false; if (data != null)
{
httpWebRequest.AllowWriteStreamBuffering = true;
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(EncodingType.GetBytes(data), , data.Length);
requestStream.Flush();
}
} HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
if (httpWebResponse != null)
{
GetResponse(ref httpResult, httpWebResponse);
httpWebResponse.Close();
}
}
catch (WebException webException)
{
GetWebExceptionResponse(ref httpResult, webException);
}
catch (Exception ex)
{
GetExceptionResponse(ref httpResult, ex, method, contentType);
}
finally
{
if (httpWebRequest != null)
{
httpWebRequest.Abort();
}
} return httpResult;
}

C# HTTP系列3 HttpWebRequest.ContentType属性的更多相关文章

  1. httpWebRequest.ContentType 属性、值 类型用法

    httpWebRequest.ContentType 属性.值 类型用法 冰火战地 指定将数据回发到服务器时浏览器使用的编码类型.下边是说明: application/x-www-form-urlen ...

  2. C# HTTP系列7 HttpWebRequest.Method属性

    系列目录     [已更新最新开发文章,点击查看详细] HttpWebRequest.Method属性,获取或设置请求的方法.用于联系 Internet 资源的请求方法. 默认值为 GET. Syst ...

  3. C# HTTP系列4 HttpWebRequest.CookieContainer属性

    系列目录     [已更新最新开发文章,点击查看详细] HttpWebRequest.CookieContainer 获取或设置与此请求关联的 Cookie.默认情况下CookieContainer  ...

  4. C# HTTP系列1 HttpWebRequest类

    系列目录     [已更新最新开发文章,点击查看详细] .NET Framework 中 System.Net 命名空间下提供了 HttpWebRequest 和 HttpWebResponse 2个 ...

  5. 问题:request.Headers;结果:HttpWebRequest.Headers 属性

    指定构成 HTTP 标头的名称/值对的集合. Headers 集合包含与请求关联的协议标头.下表列出了由系统或由属性或方法设置但未存储在 Headers 中的 HTTP 标头.   标头 设置方 Ac ...

  6. C# HTTP系列5 HttpWebResponse.StatusCode属性

    系列目录     [已更新最新开发文章,点击查看详细] HttpWebResponse.StatusCode 属性获取响应的状态.对应 HttpStatusCode 枚举值之一. HttpStatus ...

  7. Struts2 contentType属性列表

    Struts2 contentType属性列表 博客分类: Struts 2   'ez' => 'application/andrew-inset', 'hqx' => 'applica ...

  8. 深入理解javascript函数系列第三篇——属性和方法

    × 目录 [1]属性 [2]方法 前面的话 函数是javascript中的特殊的对象,可以拥有属性和方法,就像普通的对象拥有属性和方法一样.甚至可以用Function()构造函数来创建新的函数对象.本 ...

  9. HttpWebRequest.ReadWriteTimeout 属性

    获取或设置写入或读取流时的超时. 属性值在写入超时或读取超时之前的毫秒数.默认值为 300,000 毫秒(5 分钟). 备注 在写入由 GetRequestStream 方法返回的流时,或在读取由 G ...

随机推荐

  1. 升级GCC

    1. wget http://ftp.gnu.org/gnu/gcc/gcc-4.9.4/gcc-4.9.4.tar.gz 2. tar -zxvf gcc-4.9.4.tar.gz 3. cd gc ...

  2. WPF 选择文件选择文件夹

    namespace Microsoft.Win32 选择文件: if (string.IsNullOrEmpty(folderInitialDirectory)) { folderInitialDir ...

  3. c# 值类型和引用类型 笔记

    参考以下博文,我这里只是笔记一下,原文会更加详细 c#基础系列1---深入理解值类型和引用类型 堆栈和托管堆c# 值类型和引用类型 红色表示——“这啥?”(真实1个问题引出3个问题) CLR支持的两种 ...

  4. 分布式Redis深度历险-Sentinel

    上一篇介绍了Redis的主从服务器之间是如何同步数据的.试想下,在一主一从或一主多从的结构下,如果主服务器挂了,整个集群就不可用了,单点问题并没有解决.Redis使用Sentinel解决该问题,保障集 ...

  5. Docker 容器命令大全

    容器命令: 命令 描述 attach 将本地标准输入,输出和错误流转到到正在运行的容器 build 从Dockerfile构建映像 commit 根据容器的更改创建新镜像 cp 在容器和本地文件系统之 ...

  6. BUUCTF 随便注

    知识点: ##堆叠注入 #预语句注入 https://www.cnblogs.com/0nth3way/articles/7128189.html#autoid-1-0-0 正则过滤了很多关键字导致无 ...

  7. 对于不返回任何键列信息的 SelectCommand,不支持 DeleteCommand 的动态 SQL 生成

    VS新增操作数据库出现如下报错. 原因是数据库表未添加主键 MySQL: CREATE TABLE Customer (SID integer, Last_Name ), First_Name ), ...

  8. mysql udf提权实战测试

    根据前天对大牛们的资料学习,进行一次mysql udf提权测试. 测试环境: 受害者系统:centos 7.7 ,docker部署mysql5.6.46, IP:192.168.226.128 攻击者 ...

  9. centos7 扩容

    用 df -h, 看到磁盘满了 (注意:如果是二,或者第三次扩容,下面的参数就不一样) fdisk /dev/sda 但是失败了,显示没有多余的分区 可以看到 有个/dev/sda4 是我在这之前刚刚 ...

  10. JAVA List中剔除空元素(null)的方法

    方法一.list.removeAll(Collections.singleton(null)); 方法二.List nullList = new ArrayList();                ...