@@ -91,17 +91,7 @@ public override string ToString()
9191 /// <returns></returns>
9292 public static HttpStatusCode Validate ( IWebProxy webProxy , Uri targetAddress , TimeSpan ? timeout = null )
9393 {
94- if ( webProxy == null )
95- {
96- throw new ArgumentNullException ( nameof ( webProxy ) ) ;
97- }
98-
99- var httpProxy = webProxy as HttpProxy ;
100- if ( httpProxy == null )
101- {
102- httpProxy = HttpProxy . FromWebProxy ( webProxy , targetAddress ) ;
103- }
104-
94+ var httpProxy = CastToHttpProxy ( webProxy , targetAddress ) ;
10595 var remoteEndPoint = new DnsEndPoint ( httpProxy . Host , httpProxy . Port , AddressFamily . InterNetwork ) ;
10696 var socket = new Socket ( remoteEndPoint . AddressFamily , SocketType . Stream , ProtocolType . Tcp ) ;
10797
@@ -120,10 +110,7 @@ public static HttpStatusCode Validate(IWebProxy webProxy, Uri targetAddress, Tim
120110
121111 var recvBuffer = new byte [ 150 ] ;
122112 var length = socket . Receive ( recvBuffer ) ;
123-
124- var response = Encoding . ASCII . GetString ( recvBuffer , 0 , length ) ;
125- var statusCode = int . Parse ( Regex . Match ( response , "(?<=HTTP/1.1 )\\ d+" , RegexOptions . IgnoreCase ) . Value ) ;
126- return ( HttpStatusCode ) statusCode ;
113+ return ParseStatusCode ( recvBuffer , length ) ;
127114 }
128115
129116 catch ( Exception )
@@ -146,17 +133,7 @@ public static HttpStatusCode Validate(IWebProxy webProxy, Uri targetAddress, Tim
146133 /// <returns></returns>
147134 public static async Task < HttpStatusCode > ValidateAsync ( IWebProxy webProxy , Uri targetAddress , TimeSpan ? timeout = null )
148135 {
149- if ( webProxy == null )
150- {
151- throw new ArgumentNullException ( nameof ( webProxy ) ) ;
152- }
153-
154- var httpProxy = webProxy as HttpProxy ;
155- if ( httpProxy == null )
156- {
157- httpProxy = HttpProxy . FromWebProxy ( webProxy , targetAddress ) ;
158- }
159-
136+ var httpProxy = CastToHttpProxy ( webProxy , targetAddress ) ;
160137 var remoteEndPoint = new DnsEndPoint ( httpProxy . Host , httpProxy . Port , AddressFamily . InterNetwork ) ;
161138 var socket = new Socket ( remoteEndPoint . AddressFamily , SocketType . Stream , ProtocolType . Tcp ) ;
162139
@@ -171,10 +148,7 @@ public static async Task<HttpStatusCode> ValidateAsync(IWebProxy webProxy, Uri t
171148
172149 var recvBufferSegment = new ArraySegment < byte > ( new byte [ 150 ] ) ;
173150 var length = await socket . ReceiveTaskAsync ( recvBufferSegment , timeout ) ;
174-
175- var response = Encoding . ASCII . GetString ( recvBufferSegment . Array , 0 , length ) ;
176- var statusCode = int . Parse ( Regex . Match ( response , "(?<=HTTP/1.1 )\\ d+" , RegexOptions . IgnoreCase ) . Value ) ;
177- return ( HttpStatusCode ) statusCode ;
151+ return ParseStatusCode ( recvBufferSegment . Array , length ) ;
178152 }
179153 catch ( Exception )
180154 {
@@ -185,5 +159,49 @@ public static async Task<HttpStatusCode> ValidateAsync(IWebProxy webProxy, Uri t
185159 socket . Dispose ( ) ;
186160 }
187161 }
162+
163+ /// <summary>
164+ /// IWebProxy转换为HttpProxy
165+ /// </summary>
166+ /// <param name="webProxy"></param>
167+ /// <param name="targetAddress"></param>
168+ /// <returns></returns>
169+ private static HttpProxy CastToHttpProxy ( IWebProxy webProxy , Uri targetAddress )
170+ {
171+ if ( webProxy == null )
172+ {
173+ throw new ArgumentNullException ( nameof ( webProxy ) ) ;
174+ }
175+
176+ if ( targetAddress == null )
177+ {
178+ throw new ArgumentNullException ( nameof ( targetAddress ) ) ;
179+ }
180+
181+ var httpProxy = webProxy as HttpProxy ;
182+ if ( httpProxy != null )
183+ {
184+ return httpProxy ;
185+ }
186+ return HttpProxy . FromWebProxy ( webProxy , targetAddress ) ;
187+ }
188+
189+ /// <summary>
190+ /// 解析响应的状态码
191+ /// </summary>
192+ /// <param name="buffer"></param>
193+ /// <param name="length"></param>
194+ /// <returns></returns>
195+ private static HttpStatusCode ParseStatusCode ( byte [ ] buffer , int length )
196+ {
197+ var response = Encoding . ASCII . GetString ( buffer , 0 , length ) ;
198+ var match = Regex . Match ( response , "(?<=HTTP/1.1 )\\ d+" , RegexOptions . IgnoreCase ) ;
199+ if ( match . Success == false )
200+ {
201+ return HttpStatusCode . BadRequest ;
202+ }
203+ var statusCode = int . Parse ( match . Value ) ;
204+ return ( HttpStatusCode ) statusCode ;
205+ }
188206 }
189207}
0 commit comments