ServicePoint 类
地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.net.servicepoint?view=netframework-4.7.2
提供 HTTP 连接的连接管理。
下面的代码示例创建一个ServicePoint连接到 URI www.contoso.com的对象。
// This example shows how to use the ServicePoint and ServicePointManager classes.
// The ServicePointManager class uses the ServicePoint class to manage connections
// to a remote host. The networking classes reuse service points for all
// requests to a given URI. In fact, the same ServicePoint object
// is used to issue requests to Internet resources identified by the same
// scheme identifier (for example, HTTP) and host fragment (for example, www.contoso.com).
// This should improve your application performance.
// Reusing service points in this way can help improve application performance.
using System;
using System.Net;
using System.Threading;
using System.Text.RegularExpressions; namespace Mssc.Services.ConnectionManagement
{
class TestServicePoint
{
private static void ShowProperties(ServicePoint sp)
{
Console.WriteLine("Done calling FindServicePoint()..."); // Display the ServicePoint Internet resource address.
Console.WriteLine("Address = {0} ", sp.Address.ToString()); // Display the date and time that the ServicePoint was last
// connected to a host.
Console.WriteLine("IdleSince = " + sp.IdleSince.ToString()); // Display the maximum length of time that the ServicePoint instance
// is allowed to maintain an idle connection to an Internet
// resource before it is recycled for use in another connection.
Console.WriteLine("MaxIdleTime = " + sp.MaxIdleTime); Console.WriteLine("ConnectionName = " + sp.ConnectionName); // Display the maximum number of connections allowed on this
// ServicePoint instance.
Console.WriteLine("ConnectionLimit = " + sp.ConnectionLimit); // Display the number of connections associated with this
// ServicePoint instance.
Console.WriteLine("CurrentConnections = " + sp.CurrentConnections); if (sp.Certificate == null)
Console.WriteLine("Certificate = (null)");
else
Console.WriteLine("Certificate = " + sp.Certificate.ToString()); if (sp.ClientCertificate == null)
Console.WriteLine("ClientCertificate = (null)");
else
Console. WriteLine("ClientCertificate = " + sp.ClientCertificate.ToString()); Console.WriteLine("ProtocolVersion = " + sp.ProtocolVersion.ToString());
Console.WriteLine("SupportsPipelining = " + sp.SupportsPipelining); Console.WriteLine("UseNagleAlgorithm = " + sp.UseNagleAlgorithm.ToString());
Console.WriteLine("Expect 100-continue = " + sp.Expect100Continue.ToString());
} private static void makeWebRequest(int hashCode, string Uri)
{
HttpWebResponse res = null; // Make sure that the idle time has elapsed, so that a new
// ServicePoint instance is created.
Console.WriteLine("Sleeping for 2 sec.");
Thread.Sleep(2000);
try
{
// Create a request to the passed URI.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Uri); Console.WriteLine("\nConnecting to " + Uri + " ............"); // Get the response object.
res = (HttpWebResponse)req.GetResponse();
Console.WriteLine("Connected.\n"); ServicePoint currentServicePoint = req.ServicePoint; // Display new service point properties.
int currentHashCode = currentServicePoint.GetHashCode(); Console.WriteLine("New service point hashcode: " + currentHashCode);
Console.WriteLine("New service point max idle time: " + currentServicePoint.MaxIdleTime);
Console.WriteLine("New service point is idle since " + currentServicePoint.IdleSince ); // Check that a new ServicePoint instance has been created.
if (hashCode == currentHashCode)
Console.WriteLine("Service point reused.");
else
Console.WriteLine("A new service point created.") ;
}
catch (Exception e)
{
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
finally
{
if (res != null)
res.Close();
}
} // Show the user how to use this program when wrong inputs are entered.
private static void showUsage()
{
Console.WriteLine("Enter the proxy name as follows:");
Console.WriteLine("\tcs_servicepoint proxyName");
} public static void Main(string[] args)
{
int port = 80; // Define a regular expression to parse the user's input.
// This is a security check. It allows only
// alphanumeric input strings between 2 to 40 characters long.
Regex rex = new Regex(@"^[a-zA-Z]\w{1,39}$"); if (args.Length < 1)
{
showUsage();
return;
}
string proxy = args[0]; if ((rex.Match(proxy)).Success != true)
{
Console.WriteLine("Input string format not allowed.");
return;
}
string proxyAdd = "http://" + proxy + ":" + port; // Create a proxy object.
WebProxy DefaultProxy = new WebProxy(proxyAdd, true); // Set the proxy that all HttpWebRequest instances use.
WebRequest.DefaultWebProxy = DefaultProxy; // Get the base interface for proxy access for the
// WebRequest-based classes.
IWebProxy Iproxy = WebRequest.DefaultWebProxy; // Set the maximum number of ServicePoint instances to
// maintain. If a ServicePoint instance for that host already
// exists when your application requests a connection to
// an Internet resource, the ServicePointManager object
// returns this existing ServicePoint instance. If none exists
// for that host, it creates a new ServicePoint instance.
ServicePointManager.MaxServicePoints = 4; // Set the maximum idle time of a ServicePoint instance to 10 seconds.
// After the idle time expires, the ServicePoint object is eligible for
// garbage collection and cannot be used by the ServicePointManager object.
ServicePointManager.MaxServicePointIdleTime = 10000; ServicePointManager.UseNagleAlgorithm = true;
ServicePointManager.Expect100Continue = true;
ServicePointManager.CheckCertificateRevocationList = true;
ServicePointManager.DefaultConnectionLimit = ServicePointManager.DefaultPersistentConnectionLimit;
// Create the Uri object for the resource you want to access.
Uri MS = new Uri("http://msdn.microsoft.com/"); // Use the FindServicePoint method to find an existing
// ServicePoint object or to create a new one.
ServicePoint servicePoint = ServicePointManager.FindServicePoint(MS, Iproxy); ShowProperties(servicePoint); int hashCode = servicePoint.GetHashCode(); Console.WriteLine("Service point hashcode: " + hashCode); // Make a request with the same scheme identifier and host fragment
// used to create the previous ServicePoint object.
makeWebRequest(hashCode, "http://msdn.microsoft.com/library/");
}
}
}
注解
ServicePoint类根据传递给资源的统一资源标识符 (URI) 中的主机信息来处理与 Internet 资源的连接。
与资源的初始连接将确定ServicePoint对象维护的信息, 该信息随后由对该资源的所有后续请求共享。
ServicePoint对象由ServicePointManager类管理, 并根据需要ServicePointManager.FindServicePoint由方法创建。
ServicePoint永远不会直接创建对象, 而是始终由ServicePointManager类创建和管理对象。
可创建的最ServicePoint大对象数ServicePointManager.MaxServicePoints由属性设置。
每ServicePoint个对象都保持与 Internet 资源的连接, 直到它的空闲时间超过MaxIdleTime在属性中指定的时间。
ServicePoint 当MaxIdleTime超出该值时, 可以将其回收到其他连接。
的默认值MaxIdleTime ServicePointManager.MaxServicePointIdleTime由属性设置。
如果将ServicePoint属性设置为-1 以外的值, 并且在指定的时间过后, 则在服务下一个请求后将关闭活动连接。
ConnectionLeaseTimeout 这对于不需要无限期打开的活动连接的应用程序非常有用, 因为这些应用程序默认为。
-----------------------------------
demo没有调通,proxy 找不到一个合适的值;
………………
暂时未解决。
ServicePoint 类的更多相关文章
- ServicePointManager 类
地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.net.servicepointmanager?redirectedfrom=MSDN&am ...
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
- HTTPS协议学习总结
目录 一:什么是HTTPS协议?二:客户端与服务端如何建立HTTPS协议连接?三:证书.加密协议.信息摘要.签名算法概念解释与关系梳理四:低版本操作系统作为客户端发送HTTPS失败分析五:参考资料 ...
- ServicePointManager.ServerCertificateValidationCallback 冲突的解决
ServicePointManager是用于创建. 维护和删除的实例的静态类ServicePoint类. 当应用程序请求对 Internet 资源统一资源标识符 (URI) 的连接通过ServiceP ...
- .net工具类
ConvertHelper public class ConvertHelper { /// <summary> /// 转换类型 /// </summary> /// < ...
- WebUtils-网络请求工具类
网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...
- HttpWebRequest类
HttpWebRequest类与HttpRequest类的区别. HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所有信息.而HttpWebReque ...
- HttpHelper类
using System;using System.Collections.Generic;using System.Text;using System.Net;using System.IO;usi ...
- [C#HttpHelper]类1.4正式版教程与升级报告
[C#HttpHelper]类1.4正式版教程与升级报告 导读 1.升级报告 2.HttpHelper1.4正式版下载 3.HttpHelper类使用方法, 4.最简单的Post与Get的写法 ...
随机推荐
- Win10升级后无法删除Windows.old文件夹
问题:win10系统进行升级后(升级到1903版本),无法删除生成的Windows.old文件夹,提示文件夹访问被拒绝. 点击继续后,无法进行正常删除. 解决方法:使用windows自带的磁盘清理进行 ...
- Gerrit - 初始配置
1 - 插件管理 1.1 下载并安装插件 以reviewers插件为例. 在GerritForge(https://gerrit-ci.gerritforge.com/),找到对应gerrit 版本的 ...
- asp.net中的html標籤runat=server時的映射
asp.net中的html控制項runat=server時的映射 1.標準xhtml標籤:http://blog.csdn.net/TangZhongxin/archive/2009/07/31/43 ...
- Go语言【开发】加载JSON配置文件
JSON配置加载 辅助网址,JSON转结构体对应 http://json2struct.mervine.net/ 从JSON文件中加载配置到全局变量中 配置文件 config.json { &quo ...
- LOJ2401 JOISC2017 Dragon2 计算几何、线段树
传送门 先考虑每一个攻击方的龙和被攻击方的龙可以与多少个被攻击方/攻击方的龙匹配. 对于攻击方的龙\(A\)和被攻击方的龙\(B\),在道路为线段\((C,D)\)的情况下,能够与下图位置的所有对应属 ...
- stone [期望]
也许更好的阅读体验 \(\mathcal{Description}\) 有 \(n\) 堆石子,依次编号为 \(1, 2,\ldots , n\),其中第 \(i\) 堆有 \(a_i\) 颗石子 你 ...
- SpringBoot整合freemarker 引用基础
原 ElasticSearch学习笔记Ⅲ - SpringBoot整合ES 新建一个SpringBoot项目.添加es的maven坐标如下: <dependency> <groupI ...
- .net Dapper 实践系列(3) ---数据显示(Layui+Ajax+Dapper+MySQL)
目录 写在前面 产生问题 解决方案 写在前面 上一小节,我们使用Dapper 里事务实现了一对多关系的添加.这一小节,主要记录如何使用Dapper 实现多表的查询显示. 产生问题 在mvc控制器中查询 ...
- command injection命令注入
命令注入 是指程序中有调用系统命令的部分,例如输入ip,程序调用系统命令ping这个ip.如果在ip后面加一个&&.&.|.||命令拼接符号再跟上自己需要执行的系统命令 在pi ...
- MySQL中的JSON类型
前言(废话) 昨天抽了点时间在网上搜列了一个开源项目,项目挺完整的,前后台分离还带有微信小程序,我Clone下代码,经过一番倒腾,嘿~还真就跑起来了.在这个过程中,体验了一把VUE项目工程细节,因为之 ...