ASP. net mvc web API learning notes-ASP. net mvc web API learning notes-add, delete, modify, and query ASP. net mvc Web APIs

Source: Internet
Author: User
Tags microsoft website

 

1. Brief Introduction to httpclient

I vaguely remember that at that time, I used WebClient and httpwebrequest to send a request. Now ASP. NET mvc4 comes with a class httpclient, which is used to receive httpresponsemessage and send httprequestmesssage.

The problem is that since WebClient and httpwebrequest can complete the corresponding functions, why should we use the httpclient class ,. since such a class is proposed in the Net Framework, it must have its own special characteristics. Here are several differences:

(1) You can configure the extension on the httpclient instance, set the default header, and cancel unfinished requests and settings.

(2) httpclient has its own connection pool

(3) httpclient is not bound to a specific server and can access any HTTP request.

(4) httpclient uses asynchronous request processing

 

 

2. httpclient remarks

HttpclientInclude inSystem.net. http. dllProgramCentralized, accepted and sent by usersHTTPRequest. This class applies. Net4.0And later versions.

By default,Httpwebrequest  Will be used to send requests to the server. httpmessagehandler instance as parameter. "XML: Space =" preserve "style =" font-family: 'segoe UI ', verdana, Arial; font-size: 10pt; line-Height: normal; "> This behavior can specify a different channel to modify when obtaining httpmessagehandler httpmessagehandler instance as parameter. "XML: Space =" preserve "style =" font-family: 'segoe UIS ', verdana, Arial; font-size: 10pt; line-He Ight: normal; "> one of the constructor overloading of the instance is used as a parameter. webrequesthandler can be used to configure settings and the instance can be passed the constructor. "XML: Space =" preserve "style =" font-family: 'segoe UI ', verdana, Arial; font-size: 10pt; line-Height: normal; "> If features or caching with authentication are required, webrequesthandler webrequesthandler can be used to configure settings and the instance can be passed the constructor. "XML: Space =" preserve "style =" font-family: 'segoe UIS ', verdana, Arial; font-size: 10pt; Line-Height: normal; "> can be used for configuration settings, and the instance can be passed to the constructor. The returned handler is passed toHttpmessagehandler  Parameter constructor overload
For more information, refer to the official Microsoft Website:

Http://msdn.microsoft.com/zh-cn/library/system.net.http.httpclient.aspx

 

3. httpclient basic operations

Static Void Basicmethod ()
{
String Url = " Http://www.baidu.com " ;
Httpclient client = New Httpclient ();
String Content = client. getstringasync (URL). result;
Console. writeline (content );

}

The above uses httpclient to request the Baidu homepage. It seems that the operation is quite convenient. The result of running the output request is as follows:

 

 The content output by the console is the request URL.

In addition to the getstringasync () method mentioned above, httpclient also provides the following get methods. For details, refer to msdn:

 

 

4. custom request headers

To customize the request header, We need to inherit a class: httpclienthandler

Public ClassGithttpclienthandler: httpclienthandler

{
Protected Override Task {
Request. headers. referrer = New Uri ( " Http://www.google.com/ " );
Request. headers. Add ( " Useragent " , " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; wow64; Trident/5.0; slcc2;. Net CLR 2.0.50727) " );

Task Base . Sendasync (request, cancellationtoken );
Httpresponsemessage response = task. result;
Mediatypeheadervalue contenttype = response. content. headers. contenttype;
If ( String . Isnullorempty (contenttype. charset ))
{
Contenttype. charset = " GBK " ;
}
Return Task;
}
}

 Httpclienthandler is a common proxy mode. getstringasync () adds an encapsulation layer to intercept the input and output of httpclient to implement some custom operations. This method is very common in MVC and is the filter in MVC.

 

5. The request content is too long.

Httpclient has an attributeMaxresponsecontentbuffersize, which indicates that the maximum number of bytes of cache is used to read the corresponding content. The default value is 64 K. If the number of pages exceeds 64 K, an exception httprequestexception is thrown, leading to get failure.

We can manually set the size of this attribute:

Httpclient client =NewHttpclient () {maxresponsecontentbuffersize =1024*1024};

 You can use the preceding method.

 

6. Chinese problems

 Chinese problems are always a headache, and garbled characters often occur. The custom request header mentioned above can also be processed.CodeFor reference, Chinese problems are complicated and need to be handled according to specific situations:

Httpresponsemessage response = task. result;
Mediatypeheadervalue contenttype = response. content. headers. contenttype;
If ( String . Isnullorempty (contenttype. charset ))
{
Contenttype. charset = " GBK " ;

}

 

7. Links

 ASP. net mvc web API learning notes --- the first web API Program

ASP. net mvc web API learning notes --- add, delete, modify, and query contacts

ASP. net mvc web API learning notes ---- httpclient Introduction

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.