对于想了解HTTPContent-Type类型整理的读者,本文将是一篇不可错过的文章,我们将详细介绍http的contenttype,并且为您提供关于意思?、aiohttp.client_except
对于想了解HTTP Content-Type 类型整理的读者,本文将是一篇不可错过的文章,我们将详细介绍http的contenttype,并且为您提供关于意思?、aiohttp.client_exceptions.ContentTypeError: 0:json(content_type=‘??‘)、Ajax应用常见的 HTTP contentType设置、C# HTTP系列3 HttpWebRequest.ContentType属性的有价值信息。
本文目录一览:- HTTP Content-Type 类型整理(http的contenttype)
- 意思?">意思?
- aiohttp.client_exceptions.ContentTypeError: 0:json(content_type=‘??‘)
- Ajax应用常见的 HTTP contentType设置
- C# HTTP系列3 HttpWebRequest.ContentType属性
HTTP Content-Type 类型整理(http的contenttype)
HTTP协议(RFC2616)采用了请求/响应模型。客户端向服务器发送一个请求,请求头包含请求的方法、URI、协议版本、以及包含请求修饰符、客户 信息和内容的类似于MIME的消息结构。服务器以一个状态行作为响应,相应的内容包括消息协议的版本,成功或者错误编码加上包含服务器信息、实体元信息以 及可能的实体内容。
通常HTTP消息由一个起始行,一个或者多个头域,一个只是头域结束的空行和可选的消息体组成。HTTP的头域包括通用头,请求头,响应头和实体头四个部分。每个头域由一个域名,冒号(:)和域值三部分组成。域名是大小写无关的,域 值前可以添加任何数量的空格符,头域可以被扩展为多行,在每行开始处,使用至少一个空格或制表符。
请求消息和响应消息都可以包含实体信息,实体信息一般由实体头域和实体组成。实体头域包含关于实体的原信息,实体头包括Allow、Content- Base、Content-Encoding、Content-Language、 Content-Length、Content-Location、Content-MD5、Content-Range、Content-Type、 Etag、Expires、Last-Modified、extension-header。
Content-Type是返回消息中非常重要的内容,表示后面的文档属于什么MIME类型。Content-Type: [type]/[subtype]; parameter。例如最常见的就是text/html,它的意思是说返回的内容是文本类型,这个文本又是HTML格式的。原则上浏览器会根据Content-Type来决定如何显示返回的消息体内容。
type有下面的形式 :
Text:用于标准化地表示的文本信息,文本消息可以是多种字符集和或者多种格式的;
Multipart:用于连接消息体的多个部分构成一个消息,这些部分可以是不同类型的数据;
Application:用于传输应用程序数据或者二进制数据;
Message:用于包装一个E-mail消息;
Image:用于传输静态图片数据;
Audio:用于传输音频或者音声数据;
Video:用于传输动态影像数据,可以是与音频编辑在一起的视频数据格式。
subtype用于指定type的详细形式。content-type/subtype配对的集合和与此相关的参数,将随着时间而增长。为了确保这些值在一个有序而且公开的状态下开发,MIME使用Internet Assigned Numbers Authority (IANA)作为中心的注册机制来管理这些值。
parameter可以用来指定附加的信息,更多情况下是用于指定text/plain和text/htm等的文字编码方式的charset参数。MIME根据type制定了默认的subtype,当客户端不能确定消息的subtype的情况下,消息被看作默认的subtype进行处理。Text默认是text/plain,Application默认是application/octet-stream而Multipart默认情况下被看作multipart/mixed。对于IE6浏览器来说,如果Content-Type中的类型和实际的消息体类型不一致,那么它会根据内容中的类型来分析实际应该是什么类型,对于JPG、GIF等常用图片格式都可以正确的识别出来,而不管Content-Type中写的是什么。
如果Content-Type中指定的是浏览器可以直接打开的类型,那么浏览器就会直接打开其内容显示出来,如果是被关联到其它应用程序的类型,这时就要查找注册表中关于这种类型的注册情况,如果是允许直接打开而不需要询问的,就会直接调出这个关联的应用程序来打开这个文件,但如果是不允许直接打开的,就会询问是否打开。对于没有关联到任何应用程序的类型,IE浏览器不知道它该如何打开,此时IE6就会把它当成XML来尝试打开。
如果要想实现文件下载,可以在http的头中加入:
fprintf(file, "Content-Disposition:attachment; filename=\"%s\" \r\n", fileName);
经过测试,html,pdf,gif等原来在网页中打开的文件都可以正常实现下载。
''hqx'' => ''application/mac-binhex40'',
''cpt'' => ''application/mac-compactpro'',
''doc'' => ''application/msword'',
''bin'' => ''application/octet-stream'',
''dms'' => ''application/octet-stream'',
''lha'' => ''application/octet-stream'',
''lzh'' => ''application/octet-stream'',
''exe'' => ''application/octet-stream'',
''class'' => ''application/octet-stream'',
''so'' => ''application/octet-stream'',
''dll'' => ''application/octet-stream'',
''oda'' => ''application/oda'',
''pdf'' => ''application/pdf'',
''ai'' => ''application/postscript'',
''eps'' => ''application/postscript'',
''ps'' => ''application/postscript'',
''smi'' => ''application/smil'',
''smil'' => ''application/smil'',
''mif'' => ''application/vnd.mif'',
''xls'' => ''application/vnd.ms-excel'',
''ppt'' => ''application/vnd.ms-powerpoint'',
''wbxml'' => ''application/vnd.wap.wbxml'',
''wmlc'' => ''application/vnd.wap.wmlc'',
''wmlsc'' => ''application/vnd.wap.wmlscriptc'',
''bcpio'' => ''application/x-bcpio'',
''vcd'' => ''application/x-cdlink'',
''pgn'' => ''application/x-chess-pgn'',
''cpio'' => ''application/x-cpio'',
''csh'' => ''application/x-csh'',
''dcr'' => ''application/x-director'',
''dir'' => ''application/x-director'',
''dxr'' => ''application/x-director'',
''dvi'' => ''application/x-dvi'',
''spl'' => ''application/x-futuresplash'',
''gtar'' => ''application/x-gtar'',
''hdf'' => ''application/x-hdf'',
''js'' => ''application/x-javascript'',
''skp'' => ''application/x-koan'',
''skd'' => ''application/x-koan'',
''skt'' => ''application/x-koan'',
''skm'' => ''application/x-koan'',
''latex'' => ''application/x-latex'',
''nc'' => ''application/x-netcdf'',
''cdf'' => ''application/x-netcdf'',
''sh'' => ''application/x-sh'',
''shar'' => ''application/x-shar'',
''swf'' => ''application/x-shockwave-flash'',
''sit'' => ''application/x-stuffit'',
''sv4cpio'' => ''application/x-sv4cpio'',
''sv4crc'' => ''application/x-sv4crc'',
''tar'' => ''application/x-tar'',
''tcl'' => ''application/x-tcl'',
''tex'' => ''application/x-tex'',
''texinfo'' => ''application/x-texinfo'',
''texi'' => ''application/x-texinfo'',
''t'' => ''application/x-troff'',
''tr'' => ''application/x-troff'',
''roff'' => ''application/x-troff'',
''man'' => ''application/x-troff-man'',
''me'' => ''application/x-troff-me'',
''ms'' => ''application/x-troff-ms'',
''ustar'' => ''application/x-ustar'',
''src'' => ''application/x-wais-source'',
''xhtml'' => ''application/xhtml+xml'',
''xht'' => ''application/xhtml+xml'',
''zip'' => ''application/zip'',
''au'' => ''audio/basic'',
''snd'' => ''audio/basic'',
''mid'' => ''audio/midi'',
''midi'' => ''audio/midi'',
''kar'' => ''audio/midi'',
''mpga'' => ''audio/mpeg'',
''mp2'' => ''audio/mpeg'',
''mp3'' => ''audio/mpeg'',
''aif'' => ''audio/x-aiff'',
''aiff'' => ''audio/x-aiff'',
''aifc'' => ''audio/x-aiff'',
''m3u'' => ''audio/x-mpegurl'',
''ram'' => ''audio/x-pn-realaudio'',
''rm'' => ''audio/x-pn-realaudio'',
''rpm'' => ''audio/x-pn-realaudio-plugin'',
''ra'' => ''audio/x-realaudio'',
''wav'' => ''audio/x-wav'',
''pdb'' => ''chemical/x-pdb'',
''xyz'' => ''chemical/x-xyz'',
''bmp'' => ''image/bmp'',
''gif'' => ''image/gif'',
''ief'' => ''image/ief'',
''jpeg'' => ''image/jpeg'',
''jpg'' => ''image/jpeg'',
''jpe'' => ''image/jpeg'',
''png'' => ''image/png'',
''tiff'' => ''image/tiff'',
''tif'' => ''image/tiff'',
''djvu'' => ''image/vnd.djvu'',
''djv'' => ''image/vnd.djvu'',
''wbmp'' => ''image/vnd.wap.wbmp'',
''ras'' => ''image/x-cmu-raster'',
''pnm'' => ''image/x-portable-anymap'',
''pbm'' => ''image/x-portable-bitmap'',
''pgm'' => ''image/x-portable-graymap'',
''ppm'' => ''image/x-portable-pixmap'',
''rgb'' => ''image/x-rgb'',
''xbm'' => ''image/x-xbitmap'',
''xpm'' => ''image/x-xpixmap'',
''xwd'' => ''image/x-xwindowdump'',
''igs'' => ''model/iges'',
''iges'' => ''model/iges'',
''msh'' => ''model/mesh'',
''mesh'' => ''model/mesh'',
''silo'' => ''model/mesh'',
''wrl'' => ''model/vrml'',
''vrml'' => ''model/vrml'',
''css'' => ''text/css'',
''html'' => ''text/html'',
''htm'' => ''text/html'',
''asc'' => ''text/plain'',
''txt'' => ''text/plain'',
''rtx'' => ''text/richtext'',
''rtf'' => ''text/rtf'',
''sgml'' => ''text/sgml'',
''sgm'' => ''text/sgml'',
''tsv'' => ''text/tab-separated-values'',
''wml'' => ''text/vnd.wap.wml'',
''wmls'' => ''text/vnd.wap.wmlscript'',
''etx'' => ''text/x-setext'',
''xsl'' => ''text/xml'',
''xml'' => ''text/xml'',
''mpeg'' => ''video/mpeg'',
''mpg'' => ''video/mpeg'',
''mpe'' => ''video/mpeg'',
''qt'' => ''video/quicktime'',
''mov'' => ''video/quicktime'',
''mxu'' => ''video/vnd.mpegurl'',
''avi'' => ''video/x-msvideo'',
''movie'' => ''video/x-sgi-movie'',
''ice'' => ''x-conference/x-cooltalk'',
意思?" alt="意思?">
意思?">意思?
META,网页Html语言里Head区重要标签之一
HTTP-EQUIV类似于HTTP的头部协议,它回应给浏览器一些有
用的信息,以帮助正确和精确地显示网页内容。常用的HTTP-
EQUIV类型有:
1、Content-Type和Content-Language (显示字符集的设定)
说明:设定页面使用的字符集,用以说明主页制作所使用的
文字已经语言,浏览器会根据此来调用相应的字符集显示page
内容。
<Meta http-equiv="Content-Type" Content="text/html; Charset=gb2312">
该META标签定义了HTML页面所使用的字符集为GB2132,就是
国标汉字码。如果将其中的“charset=GB2312”替换成
“BIG5”,则该页面所用的字符集就是繁体中文Big5码。当你
浏览一些国外的站点时,IE浏览器会提示你要正确显示该页面
需要下载xx语支持。这个功能就是通过读取HTML页面META标签
的Content-Type属性而得知需要使用哪种字符集显示该页面
的。如果系统里没有装相应的字符集,则IE就提示下载。其他
的语言也对应不同的charset,比如日文的字符集是“iso-
2022-jp ”,韩文的是“ks_c_5601”。
Content-Type的Content还可以是:text/xml等文档类型;
Charset选项:ISO-8859-1(英文)、BIG5、UTF-8、
SHIFT-Jis、Euc、Koi8-2、us-ascii, x-mac-roman, iso-
8859-2, x-mac-ce, iso-2022-jp, x-sjis, x-euc-jp,euc-
kr, iso-2022-kr, gb2312, gb_2312-80, x-euc-tw, x-
cns11643-1,x-cns11643-2等字符集;Content-Language的
Content还可以是:EN、FR等语言代码。
aiohttp.client_exceptions.ContentTypeError: 0:json(content_type=‘??‘)
aiohttp.client_exceptions
- 问题
- 解决
问题
# 获取异步requests
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, data=data) as resp:
# 写入数据
csvWriter.writerow(await resp.json()) # 读取内容是异步的,需要挂起
print('库编号', data['p'], '爬取完毕!')
from aioHTTP_Requests import requests
async def req(URL):
headers = {"Accept": 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
"Accept-Language": "zh-CN,zh;q=0.9", "Host": "xin.baidu.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/72.0.3626.121 Safari/537.36", }
resp = await requests.get(URL,headers=headers,timeout=10,verify_ssl=False)
resp_text = await resp.json(encoding='utf-8')
return resp_text
if __name__ == "__main__":
print("HELLO WORLD")
URL = "https://xin.baidu.com/detail/basicAjax?pid=xlTM-TogKuTw9PzC-u6VwZxUBuZ5J7WMewmd"
loop = asyncio.get_event_loop()
loop.run_until_complete(req(URL))
csvWriter.writerow(await resp.json())
运行上面的程序会报错
解决
ContentTypeError:类型错误
在resp.json()
:传入编码文本类型参数即可:
resp.json(content_type='text/html',encoding='utf-8')
参考:Link
衷心感谢!
Ajax应用常见的 HTTP contentType设置
Ajax开发中,常遇到下面的几种情况:
- 服务端需要返回一段普通文本给客户端
- 服务端需要返回一段HTML代码给客户端
- 服务端需要返回一段XML代码给客户端
- 服务端需要返回一段javascript代码给客户端
- 服务端需要返回一段JSON串给客户端
对于每一种返回类型,规范的做法时要在服务端指定response的contentType。 (当然 不指定绝大多数情况下也没什么问题 尤其时返回非xml的时候
代码
普通文本:text/plain
html代码:text/html
XML代码:text/xml
javascript代码:text/javascript
JSON:application/json
图片:image/GIF或者image/JPEG
C# HTTP系列3 HttpWebRequest.ContentType属性
获取或设置请求的 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 }
今天关于HTTP Content-Type 类型整理和http的contenttype的分享就到这里,希望大家有所收获,若想了解更多关于意思?、aiohttp.client_exceptions.ContentTypeError: 0:json(content_type=‘??‘)、Ajax应用常见的 HTTP contentType设置、C# HTTP系列3 HttpWebRequest.ContentType属性等相关知识,可以在本站进行查询。
本文标签: