问题描述
他们都似乎是处理响应到客户端不同的方式。
They both seem to be different ways of handling responses to the client.
更详细地介绍我的问题:我在其中一台服务器时,我收到一个客户我想打电话给另一台服务器,并返回从第二台服务器到我的客户端的响应的请求。
More detail about my problem: I have a server in which when I receive a request from a client I want to call a second server and return the response from that second server to my client.
推荐答案
他们都达到同样的目的。
They both serve the same purpose.
-
的HttpWebRequest
/HttpWebResponse
可用,因为.NET的第一个版本,并且仍然是一个非常有效的方法。 -
的HttpClient
(使用的Htt prequestMessage
和的Htt presponseMessage
来重新present请求和响应)已被引入在.NET 4.5,并提供了一个完全异步的API,以及用于请求和响应内容的新的模型;在内部,它仍然依赖于的HttpWebRequest
/HttpWebResponse
。
HttpWebRequest
/HttpWebResponse
are available since the first version of .NET, and are still a perfectly valid approach.HttpClient
(which usesHttpRequestMessage
andHttpResponseMessage
to represent requests and responses) has been introduced in .NET 4.5, and offers a fully asynchronous API, along with a new model for request and response content; internally, it still relies onHttpWebRequest
/HttpWebResponse
.
这是很重要的区别是,的HttpWebRequest /响应
重present从一个客户点的请求和响应只,而的Htt prequestMessage / Htt的presponseMessage
可以是客户机或服务器使用(的ASP.NET Web API使用这些类型与客户端进行通信)。
An important difference is that HttpWebRequest/Response
represent the request and response from a client point of view only, whereas HttpRequestMessage/HttpResponseMessage
can be use by either a client or a server (ASP.NET Web API uses these types to communicate with the client).
您可以使用一个你最舒服的;要知道,因为的HttpClient
是异步的,使用它必须是异步的,以及在code。
You can use the one you're most comfortable with; just be aware that since HttpClient
is asynchronous, the code that uses it must be asynchronous as well.
这篇关于什么是之间的Htt presponseMessage和HttpWebResponse区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!