zoukankan      html  css  js  c++  java
  • WebClient vs HttpClient vs HttpWebRequest

    转载:http://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/

    restless

    Just when I was starting to get used to call WebServices through WSDL – like I showed here and here – I had to call a RESTful API. If you don’t know what I’m talking about you’re like me a week ago. Let’s just say that:

    • a WSDL API uses SOAP to exchange XML-encoded data
    • a REST API uses HTTP to exchange JSON-encoded data

    That’s a whole new paradigm. Instead of GetObject() and SetObject()methods you have a single url api/object that may receive either an HTTP GETrequest or an HTTP POST request.

    The .NET framework offers you three different classes to consume REST APIs: HttpWebRequestWebClientHttpClient. To worsen your analysis paralysisthe open-source community created yet another library called RestSharp. Fear not, I’ll ease your choice.

    In the beginning there was… HttpWebRequest

    d41339a1ca4823cf39fa29453d41d073186851f2a09f0b07513024e1af43ebc8

    This is the standard class that the .NET creators originally developed to consume HTTP requests. Using HttpWebRequest gives you control over every aspect of the request/response object, like timeouts, cookies, headers, protocols. Another great thing is that HttpWebRequest class does not block the user interface thread. For instance, while you’re downloading a big file from a sluggish API server, your application’s UI will remain responsive.

    However, with great power comes great complexity. In order to make a simple GET you need at least five lines of code; we’ll see WebClient does it in two.

    HttpWebRequest http = (HttpWebRequest)WebRequest.Create("http://example.com");
    WebResponse response = http.GetResponse();
    
    MemoryStream stream = response.GetResponseStream();
    StreamReader sr = new StreamReader(stream);
    string content = sr.ReadToEnd();

    The number of ways you can make a mistake with HttpWebRequest is truly astounding. Only use HttpWebRequest if you require the additional low-level control that it offers.

    WebClient. Simple.

    for_dummies_plain

    WebClient is a higher-level abstraction built on top of HttpWebRequest to simplify the most common tasks. Using WebClient is potentially slower (on the order of a few milliseconds) than using HttpWebRequest directly. But that “inefficiency” comes with huge benefits: it requires less code, is easier to use, and you’re less likely to make a mistake when using it. That same request example is now as simple as:

    var client = new WebClient();
    var text = client.DownloadString("http://example.com/page.html");

    Note: the using statements from both examples were omitted for brevity. You should definitely dispose your web request objects properly.

    Don’t worry, you can still specify timeouts, just make sure you follow this workaround.

    HttpClient, the best of both worlds

    httpclient

    HttpClient provides powerful functionality with better syntax support for newer threading features, e.g. it supports the await keyword. It also enables threaded downloads of files with better compiler checking and code validation. For a complete listing of the advantages and features of this class make sure you read this SO answer.

    The only downfall is that it requires .NET Framework 4.5, which many older or legacy machines might not have.

  • 相关阅读:
    Spring Boot从入门到精通(一)搭建第一个Spring Boot程序
    程序员未来的出路究竟在哪里?一位老码农的心声
    ​IntelliJ IDEA使用技巧—使用EasyCode插件一键生成代码04期
    浅谈Java后端开发工程师腾讯面试经历分享总结
    Java面试技巧—如何自我介绍
    互联网大厂Java面试题集—Spring boot常见面试题(二)
    互联网大厂Java面试题集—Spring boot面试题(一)
    ActiveMQ消息队列从入门到实践(4)—使用Spring JMS收发消息
    ActiveMQ消息队列从入门到实践(1)—JMS的概念和JMS消息模型
    有多少程序员干到35岁,那么其他人去干什么了?
  • 原文地址:https://www.cnblogs.com/c-x-a/p/8059064.html
Copyright © 2011-2022 走看看