Connection management (requestresponse)

zhaozj2021-02-16  47

Use HTTP to connect to the application of the data resource

ServicePoint

with

ServicePointManager

Class to manage Internet connections

The ServicePoint class provides an endpoint for the application that can connect to the endpoint to access the Internet resources. ServicePoint is identified by a Unified Resource Identifier (URI), and is classified according to the scheme identifier (such as HTTP) and the host (such as www.contoso.com).

For example, the same servicePoint instance can provide a request to http://www.contoso.com/index.htm and http://www.contoso.com/news.htm?date=today two URIs.

ServicePointManager: Manage the creation and destruction of the ServicePoint instance.

Create a servicePoint: The Internet resource requested by the program is not in the existing ServicePoint instance collection.

The ServicePoint instance will be destroyed: the instance exceeds its longest idle time, or when the existing servicePoint instance exceeds the maximum number of ServicePoint instances of the application.

Set on the ServicePointManager

MaxServicePointIdletime

with

MaxServicePoints

Attributes can control the default maximum idle time and maximum number of ServicePoint instances. default:

HttpWebRequest

Class application uses up to two two persistent connections to a given server.

=====================

Change static on the ServicePointManager class

DefaultConnectionLimit

Attributes can change the number of connections used by the application. Does not affect the previously initialized servicePoint instance.

ServicePointManager.defaultConnectionLimit = 4; // No group connection number after 4;

But then you can also set a specific connection-related property through servicePoin.ConnectionLimit.

URI URI = New URI ("http://www.contoso.com/"); servicePoint sp = servicePointManager.findServicePoint (URI); sp.connectionLimit = 5;

Join group

Connecting packets allow specific requests in a single application to associate with a defined connection pool, which is different from WebRequest.connectionGroupName.

Why use a group: assume that there is a user named JOE to access an internal Web site that displays its wage information. After authentication of JoE, the intermediate application server uses JoE credentials to connect the rear server to retrieve their wage information. The Susan also accesses the site and requests its wages. However, since the intermediate application has established a connection using JoE credentials, the back-end server responds in JOE information. If the application assigns each request request from the backend server to a connection group formed by the username, it will not accidentally sharing authentication information with other users.

SHA1Managed Sha1 = new SHA1Managed (); Byte [] updHash = Sha1.ComputeHash (Encoding.UTF8.GetBytes (UserName SecurelyStoredPassword Domain)); String secureGroupName = Encoding.Default.GetString (updHash); // Create a request for a // when this statement is executed; specific URL.WebRequest myWebRequest = WebRequest.Create ( "http://www.contoso.com"); myWebRequest.Credentials = new NetworkCredential (UserName, SecurelyStoredPassword, Domain); myWebRequest.ConnectionGroupName = secureGroupName Will connect to group

WebResponse mywebresponse = myWebRequest.getResponse ();

// INSERT The Code That Uses MyWebResponse.

Mywebresponse.close ();

If the request is assigned to the UserName variable, the requested value will generate a new server connection using the appropriate credentials.

转载请注明原文地址:https://www.9cbs.com/read-20832.html

New Post(0)