技术开发 频道

使用WCF和.NET 3.5 进行HTTP编程

  表达其他信息

  收到 HTTP GET 的应用程序会使用嵌入在 URI 中的信息来确定发送哪个资源作为回复。为了对此进行阐明,让我们来看看下面这组 URI:
 
contoso.com/artists/Flaming+Hammer/HitMe
contoso.com
/artists/Northwind/Overdone

  在这个例子中,Contoso Corporation 有一个应用程序,提供与音乐相关的资源。从这些例子中您应该可以推断出艺术家姓名以及唱片名称是如何映射到 URI 的:

contoso.com/artists/[artist]/[album]

  当应用程序收到 http://contoso.com/artists/Northwind/Overdone 的 HTTP GET 消息后,它会返回一个与 Northwind 的 Overdone 唱片相关的资源。这个 URI 显然有一定的模式。它由一些基本信息 (contoso.com/artists) 和一些 URI 段(或“孔”)组成,它们将包含艺术家和唱片的值。

  在查询字符串参数中嵌入同类信息也很普遍。虽然格式与前面的例子不同,但最终结果相同。下面是一组 URI 孔以及一个查询字符串参数:
 
contoso.com/artists/Flaming+Hammer?album=HitMe
contoso.com
/artists/Northwind?album=Overdone

  在本例中,URI 和查询字符串都使用如下的语法:
 
contoso.com/artists/[artist]?album=[album]

  HTTP 传输还会使用一组可扩展的标头,以表达其他信息。这个信息可以与缓存、消息中数据的类型、发送应用程序的名称、内容的长度、主机操作系统以及其他很多信息相关。图 1 显示了一条 HTTP GET 消息(1-8 行)以及带有若干常用标头的回复消息(9-14 行)。

  Figure 1 HTTP GET Request and Response

1    GET /PictureServices/Feed.svc/picture/Pictures;Bridge.jpg HTTP/1.1
2    Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
       application
/x-ms-application, application/vnd.ms-xpsdocument,
       application
/xaml+xml, application/x-ms-xbap,
       application
/vnd.ms-excel, application/vnd.ms-powerpoint,
       application
/msword, application/x-shockwave-flash,
       application
/x-silverlight, */*
3    Accept-Language: en-us
4    UA-CPU: x86
5    Accept-Encoding: gzip, deflate
6    User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1;
       .NET CLR 2.0.50727; InfoPath.2; .NET CLR 1.1.4322;
       .NET CLR 3.5.20706; .NET CLR 3.0.590; MS-RTC LM 8)
7    Host: www.cloudsamples.net
8    Proxy-Connection: Keep-Alive

9    HTTP/1.1 200 OK
10   Content-Type: image/jpeg
11   Server: Microsoft-IIS/7.0
12   X-Powered-By: ASP.NET
13   Date: Sat, 15 Sep 2007 18:57:11 GMT
14   Content-Length: 106333

  HTTP GET 请求中的“Accept”标头指出了客户端要接收的数据格式。正如您从那一长串值中所见,客户端可以接收多种类型的数据(图像、Office 文档、Silverlight 应用程序等)。HTTP GET 响应所含数据的格式用 Content-Type 标头的值(如第 10 行的 image/jpeg 所示)来描述。这种简单的机制可以让发送和接收应用程序协调数据格式。用标头来协调数据格式在表达能力上不及 SOAP Web 服务的 Web 服务描述语言 (WSDL) 和 XML 架构定义 (XSD) 语法,但对于 Web 来说已经足够了。
0
相关文章